aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/vim.c50
-rw-r--r--src/nvim/context.c32
-rw-r--r--src/nvim/context.h6
-rw-r--r--src/nvim/eval.c4
4 files changed, 56 insertions, 36 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 5480bb5173..602733fd31 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -1418,32 +1418,52 @@ Dictionary nvim_get_color_map(void)
/// Gets a map of the current editor state.
///
-/// @param types Context types ("regs", "jumps", "buflist", "gvars", ...)
-/// to gather, or NIL for all (see |context-types|).
+/// @param opts Optional parameters.
+/// - types: List of |context-types| ("regs", "jumps", "bufs",
+/// "gvars", …) to gather, or empty for "all".
+/// @param[out] err Error details, if any
///
/// @return map of global |context|.
-Dictionary nvim_get_context(Array types)
+Dictionary nvim_get_context(Dictionary opts, Error *err)
FUNC_API_SINCE(6)
{
- int int_types = 0;
- if (types.size == 1 && types.items[0].type == kObjectTypeNil) {
- int_types = kCtxAll;
- } else {
+ Array types = ARRAY_DICT_INIT;
+ for (size_t i = 0; i < opts.size; i++) {
+ String k = opts.items[i].key;
+ Object v = opts.items[i].value;
+ if (strequal("types", k.data)) {
+ if (v.type != kObjectTypeArray) {
+ api_set_error(err, kErrorTypeValidation, "invalid value for key: %s",
+ k.data);
+ return (Dictionary)ARRAY_DICT_INIT;
+ }
+ types = v.data.array;
+ } else {
+ api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
+ return (Dictionary)ARRAY_DICT_INIT;
+ }
+ }
+
+ int int_types = types.size > 0 ? 0 : kCtxAll;
+ if (types.size > 0) {
for (size_t i = 0; i < types.size; i++) {
if (types.items[i].type == kObjectTypeString) {
- const char *const current = types.items[i].data.string.data;
- if (strequal(current, "regs")) {
+ const char *const s = types.items[i].data.string.data;
+ if (strequal(s, "regs")) {
int_types |= kCtxRegs;
- } else if (strequal(current, "jumps")) {
+ } else if (strequal(s, "jumps")) {
int_types |= kCtxJumps;
- } else if (strequal(current, "buflist")) {
- int_types |= kCtxBuflist;
- } else if (strequal(current, "gvars")) {
+ } else if (strequal(s, "bufs")) {
+ int_types |= kCtxBufs;
+ } else if (strequal(s, "gvars")) {
int_types |= kCtxGVars;
- } else if (strequal(current, "sfuncs")) {
+ } else if (strequal(s, "sfuncs")) {
int_types |= kCtxSFuncs;
- } else if (strequal(current, "funcs")) {
+ } else if (strequal(s, "funcs")) {
int_types |= kCtxFuncs;
+ } else {
+ api_set_error(err, kErrorTypeValidation, "unexpected type: %s", s);
+ return (Dictionary)ARRAY_DICT_INIT;
}
}
}
diff --git a/src/nvim/context.c b/src/nvim/context.c
index b2a2fd3fd9..2f872ff2bf 100644
--- a/src/nvim/context.c
+++ b/src/nvim/context.c
@@ -15,7 +15,7 @@
# include "context.c.generated.h"
#endif
-int kCtxAll = (kCtxRegs | kCtxJumps | kCtxBuflist | kCtxGVars | kCtxSFuncs
+int kCtxAll = (kCtxRegs | kCtxJumps | kCtxBufs | kCtxGVars | kCtxSFuncs
| kCtxFuncs);
static ContextVec ctx_stack = KV_INITIAL_VALUE;
@@ -57,8 +57,8 @@ void ctx_free(Context *ctx)
if (ctx->jumps.data) {
msgpack_sbuffer_destroy(&ctx->jumps);
}
- if (ctx->buflist.data) {
- msgpack_sbuffer_destroy(&ctx->buflist);
+ if (ctx->bufs.data) {
+ msgpack_sbuffer_destroy(&ctx->bufs);
}
if (ctx->gvars.data) {
msgpack_sbuffer_destroy(&ctx->gvars);
@@ -90,8 +90,8 @@ void ctx_save(Context *ctx, const int flags)
ctx_save_jumps(ctx);
}
- if (flags & kCtxBuflist) {
- ctx_save_buflist(ctx);
+ if (flags & kCtxBufs) {
+ ctx_save_bufs(ctx);
}
if (flags & kCtxGVars) {
@@ -137,8 +137,8 @@ bool ctx_restore(Context *ctx, const int flags)
ctx_restore_jumps(ctx);
}
- if (flags & kCtxBuflist) {
- ctx_restore_buflist(ctx);
+ if (flags & kCtxBufs) {
+ ctx_restore_bufs(ctx);
}
if (flags & kCtxGVars) {
@@ -200,20 +200,20 @@ static inline void ctx_restore_jumps(Context *ctx)
/// Saves the buffer list to a context.
///
/// @param ctx Save to this context.
-static inline void ctx_save_buflist(Context *ctx)
+static inline void ctx_save_bufs(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- msgpack_sbuffer_init(&ctx->buflist);
- shada_encode_buflist(&ctx->buflist);
+ msgpack_sbuffer_init(&ctx->bufs);
+ shada_encode_buflist(&ctx->bufs);
}
/// Restores the buffer list from a context.
///
/// @param ctx Restore from this context.
-static inline void ctx_restore_buflist(Context *ctx)
+static inline void ctx_restore_bufs(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- shada_read_sbuf(&ctx->buflist, kShaDaWantInfo | kShaDaForceit);
+ shada_read_sbuf(&ctx->bufs, kShaDaWantInfo | kShaDaForceit);
}
/// Saves global variables to a context.
@@ -337,7 +337,7 @@ Dictionary ctx_to_dict(Context *ctx)
PUT(rv, "regs", ARRAY_OBJ(sbuf_to_array(ctx->regs)));
PUT(rv, "jumps", ARRAY_OBJ(sbuf_to_array(ctx->jumps)));
- PUT(rv, "buflist", ARRAY_OBJ(sbuf_to_array(ctx->buflist)));
+ PUT(rv, "bufs", ARRAY_OBJ(sbuf_to_array(ctx->bufs)));
PUT(rv, "gvars", ARRAY_OBJ(sbuf_to_array(ctx->gvars)));
PUT(rv, "funcs", ARRAY_OBJ(copy_array(ctx->funcs)));
@@ -367,9 +367,9 @@ int ctx_from_dict(Dictionary dict, Context *ctx)
} else if (strequal(item.key.data, "jumps")) {
types |= kCtxJumps;
ctx->jumps = array_to_sbuf(item.value.data.array);
- } else if (strequal(item.key.data, "buflist")) {
- types |= kCtxBuflist;
- ctx->buflist = array_to_sbuf(item.value.data.array);
+ } else if (strequal(item.key.data, "bufs")) {
+ types |= kCtxBufs;
+ ctx->bufs = array_to_sbuf(item.value.data.array);
} else if (strequal(item.key.data, "gvars")) {
types |= kCtxGVars;
ctx->gvars = array_to_sbuf(item.value.data.array);
diff --git a/src/nvim/context.h b/src/nvim/context.h
index 328e12c6a6..7edd63ced2 100644
--- a/src/nvim/context.h
+++ b/src/nvim/context.h
@@ -8,7 +8,7 @@
typedef struct {
msgpack_sbuffer regs; ///< Registers.
msgpack_sbuffer jumps; ///< Jumplist.
- msgpack_sbuffer buflist; ///< Buffer list.
+ msgpack_sbuffer bufs; ///< Buffer list.
msgpack_sbuffer gvars; ///< Global variables.
Array funcs; ///< Functions.
} Context;
@@ -23,7 +23,7 @@ typedef kvec_t(Context) ContextVec;
#define CONTEXT_INIT (Context) { \
.regs = MSGPACK_SBUFFER_INIT, \
.jumps = MSGPACK_SBUFFER_INIT, \
- .buflist = MSGPACK_SBUFFER_INIT, \
+ .bufs = MSGPACK_SBUFFER_INIT, \
.gvars = MSGPACK_SBUFFER_INIT, \
.funcs = ARRAY_DICT_INIT, \
}
@@ -31,7 +31,7 @@ typedef kvec_t(Context) ContextVec;
typedef enum {
kCtxRegs = 1, ///< Registers
kCtxJumps = 2, ///< Jumplist
- kCtxBuflist = 4, ///< Buffer list
+ kCtxBufs = 4, ///< Buffer list
kCtxGVars = 8, ///< Global variables
kCtxSFuncs = 16, ///< Script functions
kCtxFuncs = 32, ///< Functions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 00b9fcf646..1f753608d2 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -8002,8 +8002,8 @@ static void f_ctxpush(typval_T *argvars, typval_T *rettv, FunPtr fptr)
types |= kCtxRegs;
} else if (strequal((char *)tv_li->vval.v_string, "jumps")) {
types |= kCtxJumps;
- } else if (strequal((char *)tv_li->vval.v_string, "buflist")) {
- types |= kCtxBuflist;
+ } else if (strequal((char *)tv_li->vval.v_string, "bufs")) {
+ types |= kCtxBufs;
} else if (strequal((char *)tv_li->vval.v_string, "gvars")) {
types |= kCtxGVars;
} else if (strequal((char *)tv_li->vval.v_string, "sfuncs")) {