diff options
Diffstat (limited to 'src/nvim/api/deprecated.c')
-rw-r--r-- | src/nvim/api/deprecated.c | 288 |
1 files changed, 270 insertions, 18 deletions
diff --git a/src/nvim/api/deprecated.c b/src/nvim/api/deprecated.c index 332e2b5fc3..2ec11236d7 100644 --- a/src/nvim/api/deprecated.c +++ b/src/nvim/api/deprecated.c @@ -1,36 +1,50 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include <stdbool.h> #include <stdint.h> -#include <stdlib.h> +#include <string.h> #include "nvim/api/buffer.h" #include "nvim/api/deprecated.h" #include "nvim/api/extmark.h" +#include "nvim/api/keysets_defs.h" +#include "nvim/api/options.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" +#include "nvim/api/private/validate.h" #include "nvim/api/vimscript.h" #include "nvim/buffer_defs.h" #include "nvim/decoration.h" #include "nvim/extmark.h" +#include "nvim/func_attr.h" #include "nvim/globals.h" +#include "nvim/highlight.h" +#include "nvim/highlight_group.h" #include "nvim/lua/executor.h" #include "nvim/memory.h" -#include "nvim/pos.h" -#include "nvim/types.h" +#include "nvim/option.h" +#include "nvim/pos_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/deprecated.c.generated.h" #endif +/// @deprecated Use nvim_exec2() instead. +/// @see nvim_exec2 +String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err) + FUNC_API_SINCE(7) + FUNC_API_DEPRECATED_SINCE(11) +{ + Dict(exec_opts) opts = { .output = output }; + return exec_impl(channel_id, src, &opts, err); +} + /// @deprecated -/// @see nvim_exec +/// @see nvim_exec2 String nvim_command_output(uint64_t channel_id, String command, Error *err) FUNC_API_SINCE(1) FUNC_API_DEPRECATED_SINCE(7) { - return nvim_exec(channel_id, command, true, err); + Dict(exec_opts) opts = { .output = true }; + return exec_impl(channel_id, command, &opts, err); } /// @deprecated Use nvim_exec_lua() instead. @@ -140,25 +154,71 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, A return 0; } - Decoration *existing = decor_find_virttext(buf, (int)line, ns_id); + DecorVirtText *existing = decor_find_virttext(buf, (int)line, ns_id); if (existing) { - clear_virttext(&existing->virt_text); - existing->virt_text = virt_text; - existing->virt_text_width = width; + clear_virttext(&existing->data.virt_text); + existing->data.virt_text = virt_text; + existing->width = width; return src_id; } - Decoration decor = DECORATION_INIT; - decor.virt_text = virt_text; - decor.virt_text_width = width; - decor.priority = 0; + DecorVirtText *vt = xmalloc(sizeof *vt); + *vt = (DecorVirtText)DECOR_VIRT_TEXT_INIT; + vt->data.virt_text = virt_text; + vt->width = width; + vt->priority = 0; - extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, &decor, true, - false, kExtmarkNoUndo); + DecorInline decor = { .ext = true, .data.ext.vt = vt, .data.ext.sh_idx = DECOR_ID_INVALID }; + + extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, decor, 0, true, + false, false, false, NULL); return src_id; } +/// Gets a highlight definition by id. |hlID()| +/// +/// @deprecated use |nvim_get_hl()| instead +/// +/// @param hl_id Highlight id as returned by |hlID()| +/// @param rgb Export RGB colors +/// @param[out] err Error details, if any +/// @return Highlight definition map +/// @see nvim_get_hl_by_name +Dictionary nvim_get_hl_by_id(Integer hl_id, Boolean rgb, Arena *arena, Error *err) + FUNC_API_SINCE(3) + FUNC_API_DEPRECATED_SINCE(9) +{ + Dictionary dic = ARRAY_DICT_INIT; + VALIDATE_INT((syn_get_final_id((int)hl_id) != 0), "highlight id", hl_id, { + return dic; + }); + int attrcode = syn_id2attr((int)hl_id); + return hl_get_attr_by_id(attrcode, rgb, arena, err); +} + +/// Gets a highlight definition by name. +/// +/// @deprecated use |nvim_get_hl()| instead +/// +/// @param name Highlight group name +/// @param rgb Export RGB colors +/// @param[out] err Error details, if any +/// @return Highlight definition map +/// @see nvim_get_hl_by_id +Dictionary nvim_get_hl_by_name(String name, Boolean rgb, Arena *arena, Error *err) + FUNC_API_SINCE(3) + FUNC_API_DEPRECATED_SINCE(9) +{ + Dictionary result = ARRAY_DICT_INIT; + int id = syn_name2id(name.data); + + VALIDATE_S((id != 0), "highlight name", name.data, { + return result; + }); + return nvim_get_hl_by_id(id, rgb, arena, err); +} + /// Inserts a sequence of lines to a buffer at a certain index /// /// @deprecated use nvim_buf_set_lines(buffer, lnum, lnum, true, lines) @@ -449,3 +509,195 @@ static int64_t convert_index(int64_t index) { return index < 0 ? index - 1 : index; } + +/// Gets the option information for one option +/// +/// @deprecated Use @ref nvim_get_option_info2 instead. +/// +/// @param name Option name +/// @param[out] err Error details, if any +/// @return Option Information +Dictionary nvim_get_option_info(String name, Error *err) + FUNC_API_SINCE(7) + FUNC_API_DEPRECATED_SINCE(11) +{ + return get_vimoption(name, OPT_GLOBAL, curbuf, curwin, err); +} + +/// Sets the global value of an option. +/// +/// @deprecated +/// @param channel_id +/// @param name Option name +/// @param value New option value +/// @param[out] err Error details, if any +void nvim_set_option(uint64_t channel_id, String name, Object value, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + set_option_to(channel_id, NULL, kOptReqGlobal, name, value, err); +} + +/// Gets the global value of an option. +/// +/// @deprecated +/// @param name Option name +/// @param[out] err Error details, if any +/// @return Option value (global) +Object nvim_get_option(String name, Arena *arena, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + return get_option_from(NULL, kOptReqGlobal, name, err); +} + +/// Gets a buffer option value +/// +/// @deprecated +/// @param buffer Buffer handle, or 0 for current buffer +/// @param name Option name +/// @param[out] err Error details, if any +/// @return Option value +Object nvim_buf_get_option(Buffer buffer, String name, Arena *arena, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + buf_T *buf = find_buffer_by_handle(buffer, err); + + if (!buf) { + return (Object)OBJECT_INIT; + } + + return get_option_from(buf, kOptReqBuf, name, err); +} + +/// Sets a buffer option value. Passing `nil` as value deletes the option (only +/// works if there's a global fallback) +/// +/// @deprecated +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer +/// @param name Option name +/// @param value Option value +/// @param[out] err Error details, if any +void nvim_buf_set_option(uint64_t channel_id, Buffer buffer, String name, Object value, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + buf_T *buf = find_buffer_by_handle(buffer, err); + + if (!buf) { + return; + } + + set_option_to(channel_id, buf, kOptReqBuf, name, value, err); +} + +/// Gets a window option value +/// +/// @deprecated +/// @param window Window handle, or 0 for current window +/// @param name Option name +/// @param[out] err Error details, if any +/// @return Option value +Object nvim_win_get_option(Window window, String name, Arena *arena, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + win_T *win = find_window_by_handle(window, err); + + if (!win) { + return (Object)OBJECT_INIT; + } + + return get_option_from(win, kOptReqWin, name, err); +} + +/// Sets a window option value. Passing `nil` as value deletes the option (only +/// works if there's a global fallback) +/// +/// @deprecated +/// @param channel_id +/// @param window Window handle, or 0 for current window +/// @param name Option name +/// @param value Option value +/// @param[out] err Error details, if any +void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object value, Error *err) + FUNC_API_SINCE(1) + FUNC_API_DEPRECATED_SINCE(11) +{ + win_T *win = find_window_by_handle(window, err); + + if (!win) { + return; + } + + set_option_to(channel_id, win, kOptReqWin, name, value, err); +} + +/// Gets the value of a global or local (buffer, window) option. +/// +/// @param[in] from Pointer to buffer or window for local option value. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param name The option name. +/// @param[out] err Details of an error that may have occurred. +/// +/// @return the option value. +static Object get_option_from(void *from, OptReqScope req_scope, String name, Error *err) +{ + VALIDATE_S(name.size > 0, "option name", "<empty>", { + return (Object)OBJECT_INIT; + }); + + OptVal value = get_option_value_strict(name.data, req_scope, from, err); + if (ERROR_SET(err)) { + return (Object)OBJECT_INIT; + } + + VALIDATE_S(value.type != kOptValTypeNil, "option name", name.data, { + return (Object)OBJECT_INIT; + }); + + return optval_as_object(value); +} + +/// Sets the value of a global or local (buffer, window) option. +/// +/// @param[in] to Pointer to buffer or window for local option value. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param name The option name. +/// @param value New option value. +/// @param[out] err Details of an error that may have occurred. +static void set_option_to(uint64_t channel_id, void *to, OptReqScope req_scope, String name, + Object value, Error *err) +{ + VALIDATE_S(name.size > 0, "option name", "<empty>", { + return; + }); + + int flags = get_option_attrs(name.data); + VALIDATE_S(flags != 0, "option name", name.data, { + return; + }); + + bool error = false; + OptVal optval = object_as_optval(value, &error); + + // Handle invalid option value type. + // Don't use `name` in the error message here, because `name` can be any String. + // No need to check if value type actually matches the types for the option, as set_option_value() + // already handles that. + VALIDATE_EXP(!error, "value", "valid option type", api_typename(value.type), { + return; + }); + + // For global-win-local options -> setlocal + // For win-local options -> setglobal and setlocal (opt_flags == 0) + const int opt_flags = (req_scope == kOptReqWin && !(flags & SOPT_GLOBAL)) + ? 0 + : (req_scope == kOptReqGlobal) ? OPT_GLOBAL : OPT_LOCAL; + + WITH_SCRIPT_CONTEXT(channel_id, { + set_option_value_for(name.data, optval, opt_flags, req_scope, to, err); + }); +} |