From 543e0256c19f397921a332e06b423215fd9aecb5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 30 Nov 2023 15:51:05 +0800 Subject: build: don't define FUNC_ATTR_* as empty in headers (#26317) FUNC_ATTR_* should only be used in .c files with generated headers. Defining FUNC_ATTR_* as empty in headers causes misuses of them to be silently ignored. Instead don't define them by default, and only define them as empty after a .c file has included its generated header. --- src/nvim/api/options.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index c012a69c7b..9cf91bad42 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -11,7 +11,6 @@ #include "nvim/autocmd.h" #include "nvim/buffer.h" #include "nvim/eval/window.h" -#include "nvim/func_attr.h" #include "nvim/globals.h" #include "nvim/macros_defs.h" #include "nvim/memory.h" -- cgit From e718866358652020e57fdb43bd9322f0c5732432 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Wed, 6 Dec 2023 13:39:36 +0600 Subject: refactor(options): remove SOPT type enums (#26417) Problem: SOPT type enums (`SOPT_NUM`, `SOPT_BOOL`, `SOPT_STRING`) are no longer used anywhere. Solution: Remove them. --- src/nvim/api/options.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 9cf91bad42..ef7c11131a 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -400,7 +400,7 @@ int get_option_attrs(char *name) vimoption_T *opt = get_option(opt_idx); if (is_tty_option(opt->fullname)) { - return SOPT_STRING | SOPT_GLOBAL; + return SOPT_GLOBAL; } // Hidden option @@ -410,14 +410,6 @@ int get_option_attrs(char *name) int attrs = 0; - if (opt->flags & P_BOOL) { - attrs |= SOPT_BOOL; - } else if (opt->flags & P_NUM) { - attrs |= SOPT_NUM; - } else if (opt->flags & P_STRING) { - attrs |= SOPT_STRING; - } - if (opt->indir == PV_NONE || (opt->indir & PV_BOTH)) { attrs |= SOPT_GLOBAL; } -- cgit From 9ae7d36ff5ebaf75597b442e10890bd77df01fbe Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Thu, 7 Dec 2023 00:40:48 +0600 Subject: refactor(options): split `get_option_value()` into smaller functions Problem: Currently, `get_option_value()` returns 3 separate things: The actual value of the option, whether the option is hidden, and the option flags. This makes the function difficult to refactor, modify or otherwise reason about. Solution: Split `get_option_value()` into 3 functions, each with a single purpose. This also affects `get_option_value_for()`. --- src/nvim/api/options.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index ef7c11131a..53fd8af8b5 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -152,23 +152,20 @@ 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) { - Object rv = OBJECT_INIT; - OptVal value = NIL_OPTVAL; - int scope = 0; OptReqScope req_scope = kOptReqGlobal; void *from = NULL; char *filetype = NULL; if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, &filetype, err)) { - goto err; + return (Object)OBJECT_INIT; } aco_save_T aco; buf_T *ftbuf = do_ft_buf(filetype, &aco, err); if (ERROR_SET(err)) { - goto err; + return (Object)OBJECT_INIT; } if (ftbuf != NULL) { @@ -176,8 +173,9 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) from = ftbuf; } - bool hidden; - value = get_option_value_for(name.data, NULL, scope, &hidden, req_scope, from, err); + int opt_idx = findoption(name.data); + OptVal value = get_option_value_for(opt_idx, scope, req_scope, from, err); + bool hidden = is_option_hidden(opt_idx); if (ftbuf != NULL) { // restore curwin/curbuf and a few other things @@ -198,7 +196,7 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) return optval_as_object(value); err: optval_free(value); - return rv; + return (Object)OBJECT_INIT; } /// Sets the value of an option. The behavior of this function matches that of @@ -391,6 +389,10 @@ static void restore_option_context(void *const ctx, OptReqScope req_scope) /// See SOPT_* in option_defs.h for other flags. int get_option_attrs(char *name) { + if (is_tty_option(name)) { + return SOPT_GLOBAL; + } + int opt_idx = findoption(name); if (opt_idx < 0) { @@ -399,10 +401,6 @@ int get_option_attrs(char *name) vimoption_T *opt = get_option(opt_idx); - if (is_tty_option(opt->fullname)) { - return SOPT_GLOBAL; - } - // Hidden option if (opt->var == NULL) { return 0; @@ -470,14 +468,11 @@ static bool option_has_scope(char *name, OptReqScope req_scope) /// buffer-local value depending on opt_scope). OptVal get_option_value_strict(char *name, OptReqScope req_scope, void *from, Error *err) { - OptVal retv = NIL_OPTVAL; - if (!option_has_scope(name, req_scope)) { - return retv; + return NIL_OPTVAL; } - if (get_tty_option(name, &retv.data.string.data)) { - retv.type = kOptValTypeString; - return retv; + if (is_tty_option(name)) { + return get_tty_option(name); } int opt_idx = findoption(name); @@ -490,11 +485,11 @@ OptVal get_option_value_strict(char *name, OptReqScope req_scope, void *from, Er : (req_scope == kOptReqBuf ? (void *)&aco : NULL); bool switched = switch_option_context(ctx, req_scope, from, err); if (ERROR_SET(err)) { - return retv; + return NIL_OPTVAL; } char *varp = get_varp_scope(opt, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL); - retv = optval_from_varp(opt_idx, varp); + OptVal retv = optval_from_varp(opt_idx, varp); if (switched) { restore_option_context(ctx, req_scope); @@ -505,7 +500,7 @@ OptVal get_option_value_strict(char *name, OptReqScope req_scope, void *from, Er /// Get option value for buffer / window. /// -/// @param[in] name Option name. +/// @param opt_idx Option index in options[] table. /// @param[out] flagsp Set to the option flags (P_xxxx) (if not NULL). /// @param[in] scope Option scope (can be OPT_LOCAL, OPT_GLOBAL or a combination). /// @param[out] hidden Whether option is hidden. @@ -514,8 +509,8 @@ OptVal get_option_value_strict(char *name, OptReqScope req_scope, void *from, Er /// @param[out] err Error message, if any. /// /// @return Option value. Must be freed by caller. -OptVal get_option_value_for(const char *const name, uint32_t *flagsp, int scope, bool *hidden, - const OptReqScope req_scope, void *const from, Error *err) +OptVal get_option_value_for(int opt_idx, int scope, const OptReqScope req_scope, void *const from, + Error *err) { switchwin_T switchwin; aco_save_T aco; @@ -527,7 +522,7 @@ OptVal get_option_value_for(const char *const name, uint32_t *flagsp, int scope, return NIL_OPTVAL; } - OptVal retv = get_option_value(name, flagsp, scope, hidden); + OptVal retv = get_option_value(opt_idx, scope); if (switched) { restore_option_context(ctx, req_scope); -- cgit From 6346987601a28b00564295ee8be0a8b00d9ff911 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Thu, 7 Dec 2023 23:46:57 +0600 Subject: refactor(options): reduce `findoption()` usage Problem: Many places in the code use `findoption()` to access an option using its name, even if the option index is available. This is very slow because it requires looping through the options array over and over. Solution: Use option index instead of name wherever possible. Also introduce an `OptIndex` enum which contains the index for every option as enum constants, this eliminates the need to pass static option names as strings. --- src/nvim/api/options.c | 65 ++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 36 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 53fd8af8b5..0d18c7871c 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -23,7 +23,7 @@ # include "api/options.c.generated.h" #endif -static int validate_option_value_args(Dict(option) *opts, char *name, int *scope, +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) { @@ -79,7 +79,8 @@ static int validate_option_value_args(Dict(option) *opts, char *name, int *scope return FAIL; }); - int flags = get_option_attrs(name); + *opt_idx = findoption(name); + int flags = get_option_attrs(*opt_idx); if (flags == 0) { // hidden or unknown option api_set_error(err, kErrorTypeValidation, "Unknown option '%s'", name); @@ -119,10 +120,10 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err) aucmd_prepbuf(aco, ftbuf); TRY_WRAP(err, { - set_option_value("bufhidden", STATIC_CSTR_AS_OPTVAL("hide"), OPT_LOCAL); - set_option_value("buftype", STATIC_CSTR_AS_OPTVAL("nofile"), OPT_LOCAL); - set_option_value("swapfile", BOOLEAN_OPTVAL(false), OPT_LOCAL); - set_option_value("modeline", BOOLEAN_OPTVAL(false), OPT_LOCAL); // 'nomodeline' + set_option_value(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("hide"), OPT_LOCAL); + set_option_value(kOptBuftype, STATIC_CSTR_AS_OPTVAL("nofile"), OPT_LOCAL); + set_option_value(kOptSwapfile, BOOLEAN_OPTVAL(false), OPT_LOCAL); + set_option_value(kOptModeline, BOOLEAN_OPTVAL(false), OPT_LOCAL); // 'nomodeline' ftbuf->b_p_ft = xstrdup(filetype); do_filetype_autocmd(ftbuf, false); @@ -152,12 +153,14 @@ 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; int scope = 0; OptReqScope req_scope = kOptReqGlobal; void *from = NULL; char *filetype = NULL; - if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, &filetype, err)) { + if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &from, &filetype, + err)) { return (Object)OBJECT_INIT; } @@ -173,7 +176,6 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) from = ftbuf; } - int opt_idx = findoption(name.data); OptVal value = get_option_value_for(opt_idx, scope, req_scope, from, err); bool hidden = is_option_hidden(opt_idx); @@ -217,10 +219,11 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict( Error *err) FUNC_API_SINCE(9) { + int opt_idx = 0; int scope = 0; OptReqScope req_scope = kOptReqGlobal; void *to = NULL; - if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &to, NULL, err)) { + if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &to, NULL, err)) { return; } @@ -231,7 +234,7 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict( // // Then force scope to local since we don't want to change the global option if (req_scope == kOptReqWin && scope == 0) { - int flags = get_option_attrs(name.data); + int flags = get_option_attrs(opt_idx); if (flags & SOPT_GLOBAL) { scope = OPT_LOCAL; } @@ -249,7 +252,7 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict( }); WITH_SCRIPT_CONTEXT(channel_id, { - set_option_value_for(name.data, optval, scope, req_scope, to, err); + set_option_value_for(name.data, opt_idx, optval, scope, req_scope, to, err); }); } @@ -303,10 +306,12 @@ 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; int scope = 0; OptReqScope req_scope = kOptReqGlobal; void *from = NULL; - if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, NULL, err)) { + if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &from, NULL, + err)) { return (Dictionary)ARRAY_DICT_INIT; } @@ -382,19 +387,13 @@ static void restore_option_context(void *const ctx, OptReqScope req_scope) /// Get attributes for an option. /// -/// @param name Option name. +/// @param opt_idx Option index in options[] table. /// /// @return Option attributes. /// 0 for hidden or unknown option. /// See SOPT_* in option_defs.h for other flags. -int get_option_attrs(char *name) +int get_option_attrs(int opt_idx) { - if (is_tty_option(name)) { - return SOPT_GLOBAL; - } - - int opt_idx = findoption(name); - if (opt_idx < 0) { return 0; } @@ -422,14 +421,12 @@ int get_option_attrs(char *name) /// Check if option has a value in the requested scope. /// -/// @param name Option name. +/// @param opt_idx Option index in options[] table. /// @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(char *name, OptReqScope req_scope) +static bool option_has_scope(int opt_idx, OptReqScope req_scope) { - int opt_idx = findoption(name); - if (opt_idx < 0) { return false; } @@ -458,7 +455,7 @@ static bool option_has_scope(char *name, OptReqScope req_scope) /// Get the option value in the requested scope. /// -/// @param name Option name. +/// @param opt_idx Option index in options[] table. /// @param req_scope Requested option scope. See OptReqScope in option.h. /// @param[in] from Pointer to buffer or window for local option value. /// @param[out] err Error message, if any. @@ -466,17 +463,11 @@ static bool option_has_scope(char *name, 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(char *name, OptReqScope req_scope, void *from, Error *err) +OptVal get_option_value_strict(int opt_idx, OptReqScope req_scope, void *from, Error *err) { - if (!option_has_scope(name, req_scope)) { + if (opt_idx < 0 || !option_has_scope(opt_idx, req_scope)) { return NIL_OPTVAL; } - if (is_tty_option(name)) { - return get_tty_option(name); - } - - int opt_idx = findoption(name); - assert(opt_idx != 0); // option_has_scope() already verifies if option name is valid. vimoption_T *opt = get_option(opt_idx); switchwin_T switchwin; @@ -533,14 +524,16 @@ OptVal get_option_value_for(int opt_idx, int scope, const OptReqScope req_scope, /// Set option value for buffer / window. /// -/// @param[in] name Option name. +/// @param name Option name. +/// @param opt_idx Option index in options[] table. /// @param[in] value Option value. /// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both). /// @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 *const name, OptVal value, const int opt_flags, +void set_option_value_for(const char *name, int opt_idx, OptVal value, const int opt_flags, const OptReqScope req_scope, void *const from, Error *err) + FUNC_ATTR_NONNULL_ARG(1) { switchwin_T switchwin; aco_save_T aco; @@ -552,7 +545,7 @@ void set_option_value_for(const char *const name, OptVal value, const int opt_fl return; } - const char *const errmsg = set_option_value(name, value, opt_flags); + const char *const errmsg = set_option_value_handle_tty(name, opt_idx, value, opt_flags); if (errmsg) { api_set_error(err, kErrorTypeException, "%s", errmsg); } -- cgit From bf3bc1cec9f00b9644815001a8732ecedf3ce07f Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Thu, 7 Dec 2023 23:59:30 +0600 Subject: refactor(options): convert `opt_idx` variables to `OptIndex` --- src/nvim/api/options.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/nvim/api/options.c') 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) { -- cgit From 8f08b1efbd096850c04c2e8e2890d993bd4d9f95 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Sun, 17 Dec 2023 05:23:33 +0600 Subject: refactor(options): use hashy for finding options (#26573) Problem: `findoption()` searches through the options[] table linearly for option names, even though hashy can be used to generate a compile-time hash table for it. Solution: Use hashy to generate a compile time hash table for finding options. This also allows handling option aliases, so we don't need separate options[] table entries for things like 'viminfo'. --- src/nvim/api/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 54a2fbf385..a3ea7d9d47 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -79,7 +79,7 @@ static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex * return FAIL; }); - *opt_idxp = findoption(name); + *opt_idxp = find_option(name); int flags = get_option_attrs(*opt_idxp); if (flags == 0) { // hidden or unknown option -- cgit From 69bc519b53ebf78fd95c8256468e7d538ebcb948 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Tue, 12 Dec 2023 15:40:21 +0100 Subject: refactor: move non-symbols to defs.h headers --- src/nvim/api/options.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index a3ea7d9d47..ea617dffaf 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -15,7 +15,6 @@ #include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/option.h" -#include "nvim/option_vars.h" #include "nvim/vim_defs.h" #include "nvim/window.h" -- cgit From 0c120307ca1ab613e63865c634d7e10ad67fb0ba Mon Sep 17 00:00:00 2001 From: dundargoc Date: Wed, 20 Dec 2023 14:32:22 +0100 Subject: refactor: eliminate cyclic includes --- src/nvim/api/options.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index ea617dffaf..fb461152b4 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -15,6 +15,7 @@ #include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/option.h" +#include "nvim/types_defs.h" #include "nvim/vim_defs.h" #include "nvim/window.h" -- cgit From 4d98ec2fa45a100292c490acc606dfb55a0573c1 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Mon, 18 Dec 2023 04:10:41 +0600 Subject: refactor(options): move some functions from options.c to option.c --- src/nvim/api/options.c | 235 ------------------------------------------------- 1 file changed, 235 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index fb461152b4..ff1741558d 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -10,7 +10,6 @@ #include "nvim/api/private/validate.h" #include "nvim/autocmd.h" #include "nvim/buffer.h" -#include "nvim/eval/window.h" #include "nvim/globals.h" #include "nvim/macros_defs.h" #include "nvim/memory.h" @@ -320,237 +319,3 @@ Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err) return get_vimoption(name, scope, buf, win, err); } - -/// Switch current context to get/set option value for window/buffer. -/// -/// @param[out] ctx Current context. switchwin_T for window and aco_save_T for buffer. -/// @param req_scope Requested option scope. See OptReqScope in option.h. -/// @param[in] from Target buffer/window. -/// @param[out] err Error message, if any. -/// -/// @return true if context was switched, false otherwise. -static bool switch_option_context(void *const ctx, OptReqScope req_scope, void *const from, - Error *err) -{ - switch (req_scope) { - case kOptReqWin: { - win_T *const win = (win_T *)from; - switchwin_T *const switchwin = (switchwin_T *)ctx; - - if (win == curwin) { - return false; - } - - if (switch_win_noblock(switchwin, win, win_find_tabpage(win), true) - == FAIL) { - restore_win_noblock(switchwin, true); - - if (try_end(err)) { - return false; - } - api_set_error(err, kErrorTypeException, "Problem while switching windows"); - return false; - } - return true; - } - case kOptReqBuf: { - buf_T *const buf = (buf_T *)from; - aco_save_T *const aco = (aco_save_T *)ctx; - - if (buf == curbuf) { - return false; - } - aucmd_prepbuf(aco, buf); - return true; - } - case kOptReqGlobal: - return false; - } - UNREACHABLE; -} - -/// Restore context after getting/setting option for window/buffer. See switch_option_context() for -/// params. -static void restore_option_context(void *const ctx, OptReqScope req_scope) -{ - switch (req_scope) { - case kOptReqWin: - restore_win_noblock((switchwin_T *)ctx, true); - break; - case kOptReqBuf: - aucmd_restbuf((aco_save_T *)ctx); - break; - case kOptReqGlobal: - break; - } -} - -/// Get attributes for an option. -/// -/// @param opt_idx Option index in options[] table. -/// -/// @return Option attributes. -/// 0 for hidden or unknown option. -/// See SOPT_* in option_defs.h for other flags. -int get_option_attrs(OptIndex opt_idx) -{ - if (opt_idx == kOptInvalid) { - return 0; - } - - vimoption_T *opt = get_option(opt_idx); - - // Hidden option - if (opt->var == NULL) { - return 0; - } - - int attrs = 0; - - if (opt->indir == PV_NONE || (opt->indir & PV_BOTH)) { - attrs |= SOPT_GLOBAL; - } - if (opt->indir & PV_WIN) { - attrs |= SOPT_WIN; - } else if (opt->indir & PV_BUF) { - attrs |= SOPT_BUF; - } - - return attrs; -} - -/// Check if option has a value in the requested scope. -/// -/// @param opt_idx Option index in options[] table. -/// @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(OptIndex opt_idx, OptReqScope req_scope) -{ - if (opt_idx == kOptInvalid) { - return false; - } - - vimoption_T *opt = get_option(opt_idx); - - // Hidden option. - if (opt->var == NULL) { - return false; - } - // TTY option. - if (is_tty_option(opt->fullname)) { - return req_scope == kOptReqGlobal; - } - - switch (req_scope) { - case kOptReqGlobal: - return opt->var != VAR_WIN; - case kOptReqBuf: - return opt->indir & PV_BUF; - case kOptReqWin: - return opt->indir & PV_WIN; - } - UNREACHABLE; -} - -/// Get the option value in the requested scope. -/// -/// @param opt_idx Option index in options[] table. -/// @param req_scope Requested option scope. See OptReqScope in option.h. -/// @param[in] from Pointer to buffer or window for local option value. -/// @param[out] err Error message, if any. -/// -/// @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(OptIndex opt_idx, OptReqScope req_scope, void *from, Error *err) -{ - if (opt_idx == kOptInvalid || !option_has_scope(opt_idx, req_scope)) { - return NIL_OPTVAL; - } - - vimoption_T *opt = get_option(opt_idx); - switchwin_T switchwin; - aco_save_T aco; - void *ctx = req_scope == kOptReqWin ? (void *)&switchwin - : (req_scope == kOptReqBuf ? (void *)&aco : NULL); - bool switched = switch_option_context(ctx, req_scope, from, err); - if (ERROR_SET(err)) { - return NIL_OPTVAL; - } - - char *varp = get_varp_scope(opt, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL); - OptVal retv = optval_from_varp(opt_idx, varp); - - if (switched) { - restore_option_context(ctx, req_scope); - } - - return retv; -} - -/// Get option value for buffer / window. -/// -/// @param opt_idx Option index in options[] table. -/// @param[out] flagsp Set to the option flags (P_xxxx) (if not NULL). -/// @param[in] scope Option scope (can be OPT_LOCAL, OPT_GLOBAL or a combination). -/// @param[out] hidden Whether option is hidden. -/// @param req_scope Requested option scope. See OptReqScope in option.h. -/// @param[in] from Target buffer/window. -/// @param[out] err Error message, if any. -/// -/// @return Option value. Must be freed by caller. -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; - void *ctx = req_scope == kOptReqWin ? (void *)&switchwin - : (req_scope == kOptReqBuf ? (void *)&aco : NULL); - - bool switched = switch_option_context(ctx, req_scope, from, err); - if (ERROR_SET(err)) { - return NIL_OPTVAL; - } - - OptVal retv = get_option_value(opt_idx, scope); - - if (switched) { - restore_option_context(ctx, req_scope); - } - - return retv; -} - -/// Set option value for buffer / window. -/// -/// @param name Option name. -/// @param opt_idx Option index in options[] table. -/// @param[in] value Option value. -/// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both). -/// @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, OptIndex opt_idx, OptVal value, const int opt_flags, - const OptReqScope req_scope, void *const from, Error *err) - FUNC_ATTR_NONNULL_ARG(1) -{ - switchwin_T switchwin; - aco_save_T aco; - void *ctx = req_scope == kOptReqWin ? (void *)&switchwin - : (req_scope == kOptReqBuf ? (void *)&aco : NULL); - - bool switched = switch_option_context(ctx, req_scope, from, err); - if (ERROR_SET(err)) { - return; - } - - const char *const errmsg = set_option_value_handle_tty(name, opt_idx, value, opt_flags); - if (errmsg) { - api_set_error(err, kErrorTypeException, "%s", errmsg); - } - - if (switched) { - restore_option_context(ctx, req_scope); - } -} -- cgit From 1813661a6197c76ea6621284570aca1d56597099 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Thu, 4 Jan 2024 15:38:16 +0100 Subject: refactor(IWYU): fix headers Remove `export` pramgas from defs headers as it causes IWYU to believe that the definitions from the defs headers comes from main header, which is not what we really want. --- src/nvim/api/options.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index ff1741558d..fce4a85804 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -9,14 +9,14 @@ #include "nvim/api/private/helpers.h" #include "nvim/api/private/validate.h" #include "nvim/autocmd.h" +#include "nvim/autocmd_defs.h" #include "nvim/buffer.h" +#include "nvim/buffer_defs.h" #include "nvim/globals.h" -#include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/option.h" #include "nvim/types_defs.h" #include "nvim/vim_defs.h" -#include "nvim/window.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/options.c.generated.h" -- cgit From 2d0e29614b4417e9764b9f7d588a50fe7f752749 Mon Sep 17 00:00:00 2001 From: bfredl Date: Fri, 9 Feb 2024 11:35:02 +0100 Subject: refactor(api): use arena for nvim_get_option_info() --- src/nvim/api/options.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index fce4a85804..8128fdf67b 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -263,10 +263,10 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict( /// @see |nvim_get_commands()| /// /// @return dictionary of all options -Dictionary nvim_get_all_options_info(Error *err) +Dictionary nvim_get_all_options_info(Arena *arena, Error *err) FUNC_API_SINCE(7) { - return get_all_vimoptions(); + return get_all_vimoptions(arena); } /// Gets the option information for one option from arbitrary buffer or window @@ -302,7 +302,7 @@ Dictionary nvim_get_all_options_info(Error *err) /// Implies {scope} is "local". /// @param[out] err Error details, if any /// @return Option Information -Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err) +Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Arena *arena, Error *err) FUNC_API_SINCE(11) { OptIndex opt_idx = 0; @@ -317,5 +317,5 @@ Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err) buf_T *buf = (req_scope == kOptReqBuf) ? (buf_T *)from : curbuf; win_T *win = (req_scope == kOptReqWin) ? (win_T *)from : curwin; - return get_vimoption(name, scope, buf, win, err); + return get_vimoption(name, scope, buf, win, arena, err); } -- cgit From 3cc54586be7760652e8bad88cae82ce74ef9432e Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 20 Feb 2024 13:44:50 +0100 Subject: refactor(api): make freeing of return-value opt-in instead of opt out As only a few API functions make use of explicit freeing of the return value, make it opt-in instead. The arena is always present under the hood, so `Arena *arena` arg now doesn't mean anything other than getting access to this arena. Also it is in principle possible to return an allocated value while still using the arena as scratch space for other stuff (unlikely, but there no reason to not allow it). --- src/nvim/api/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 8128fdf67b..7deadb8eb5 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -150,7 +150,7 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err) /// @param[out] err Error details, if any /// @return Option value Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) - FUNC_API_SINCE(9) + FUNC_API_SINCE(9) FUNC_API_RET_ALLOC { OptIndex opt_idx = 0; int scope = 0; -- cgit From 9beb40a4db5613601fc1a4b828a44e5977eca046 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 15 Feb 2024 17:16:04 +0000 Subject: feat(docs): replace lua2dox.lua Problem: The documentation flow (`gen_vimdoc.py`) has several issues: - it's not very versatile - depends on doxygen - doesn't work well with Lua code as it requires an awkward filter script to convert it into pseudo-C. - The intermediate XML files and filters makes it too much like a rube goldberg machine. Solution: Re-implement the flow using Lua, LPEG and treesitter. - `gen_vimdoc.py` is now replaced with `gen_vimdoc.lua` and replicates a portion of the logic. - `lua2dox.lua` is gone! - No more XML files. - Doxygen is now longer used and instead we now use: - LPEG for comment parsing (see `scripts/luacats_grammar.lua` and `scripts/cdoc_grammar.lua`). - LPEG for C parsing (see `scripts/cdoc_parser.lua`) - Lua patterns for Lua parsing (see `scripts/luacats_parser.lua`). - Treesitter for Markdown parsing (see `scripts/text_utils.lua`). - The generated `runtime/doc/*.mpack` files have been removed. - `scripts/gen_eval_files.lua` now instead uses `scripts/cdoc_parser.lua` directly. - Text wrapping is implemented in `scripts/text_utils.lua` and appears to produce more consistent results (the main contributer to the diff of this change). --- src/nvim/api/options.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/nvim/api/options.c') diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 7deadb8eb5..d9bc0ccc92 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -272,21 +272,21 @@ Dictionary nvim_get_all_options_info(Arena *arena, Error *err) /// Gets the option information for one option from arbitrary buffer or window /// /// Resulting dictionary has keys: -/// - name: Name of the option (like 'filetype') -/// - shortname: Shortened name of the option (like 'ft') -/// - type: type of option ("string", "number" or "boolean") -/// - default: The default value for the option -/// - was_set: Whether the option was set. +/// - name: Name of the option (like 'filetype') +/// - shortname: Shortened name of the option (like 'ft') +/// - type: type of option ("string", "number" or "boolean") +/// - default: The default value for the option +/// - was_set: Whether the option was set. /// -/// - last_set_sid: Last set script id (if any) -/// - last_set_linenr: line number where option was set -/// - last_set_chan: Channel where option was set (0 for local) +/// - last_set_sid: Last set script id (if any) +/// - last_set_linenr: line number where option was set +/// - last_set_chan: Channel where option was set (0 for local) /// -/// - scope: one of "global", "win", or "buf" -/// - global_local: whether win or buf option has a global value +/// - scope: one of "global", "win", or "buf" +/// - global_local: whether win or buf option has a global value /// -/// - commalist: List of comma separated values -/// - flaglist: List of single char flags +/// - commalist: List of comma separated values +/// - flaglist: List of single char flags /// /// When {scope} is not provided, the last set information applies to the local /// value in the current buffer or window if it is available, otherwise the -- cgit