helpers.h
Go to the documentation of this file.
1 #ifndef NVIM_API_PRIVATE_HELPERS_H
2 #define NVIM_API_PRIVATE_HELPERS_H
3 
4 #include <stdbool.h>
5 
7 #include "nvim/vim.h"
8 #include "nvim/getchar.h"
9 #include "nvim/memory.h"
10 #include "nvim/ex_eval.h"
11 #include "nvim/lib/kvec.h"
12 
13 #define OBJECT_OBJ(o) o
14 
15 #define BOOLEAN_OBJ(b) ((Object) { \
16  .type = kObjectTypeBoolean, \
17  .data.boolean = b })
18 
19 #define INTEGER_OBJ(i) ((Object) { \
20  .type = kObjectTypeInteger, \
21  .data.integer = i })
22 
23 #define FLOAT_OBJ(f) ((Object) { \
24  .type = kObjectTypeFloat, \
25  .data.floating = f })
26 
27 #define STRING_OBJ(s) ((Object) { \
28  .type = kObjectTypeString, \
29  .data.string = s })
30 
31 #define BUFFER_OBJ(s) ((Object) { \
32  .type = kObjectTypeBuffer, \
33  .data.integer = s })
34 
35 #define WINDOW_OBJ(s) ((Object) { \
36  .type = kObjectTypeWindow, \
37  .data.integer = s })
38 
39 #define TABPAGE_OBJ(s) ((Object) { \
40  .type = kObjectTypeTabpage, \
41  .data.integer = s })
42 
43 #define ARRAY_OBJ(a) ((Object) { \
44  .type = kObjectTypeArray, \
45  .data.array = a })
46 
47 #define DICTIONARY_OBJ(d) ((Object) { \
48  .type = kObjectTypeDictionary, \
49  .data.dictionary = d })
50 
51 #define LUAREF_OBJ(r) ((Object) { \
52  .type = kObjectTypeLuaRef, \
53  .data.luaref = r })
54 
55 #define NIL ((Object) {.type = kObjectTypeNil})
56 
57 #define PUT(dict, k, v) \
58  kv_push(dict, ((KeyValuePair) { .key = cstr_to_string(k), .value = v }))
59 
60 #define ADD(array, item) \
61  kv_push(array, item)
62 
63 #define FIXED_TEMP_ARRAY(name, fixsize) \
64  Array name = ARRAY_DICT_INIT; \
65  Object name##__items[fixsize]; \
66  name.size = fixsize; \
67  name.items = name##__items; \
68 
69 #define STATIC_CSTR_AS_STRING(s) ((String) {.data = s, .size = sizeof(s) - 1})
70 
74 #define STATIC_CSTR_TO_STRING(s) ((String){ \
75  .data = xmemdupz(s, sizeof(s) - 1), \
76  .size = sizeof(s) - 1 })
77 
78 // Helpers used by the generated msgpack-rpc api wrappers
79 #define api_init_boolean
80 #define api_init_integer
81 #define api_init_float
82 #define api_init_string = STRING_INIT
83 #define api_init_buffer
84 #define api_init_window
85 #define api_init_tabpage
86 #define api_init_object = NIL
87 #define api_init_array = ARRAY_DICT_INIT
88 #define api_init_dictionary = ARRAY_DICT_INIT
89 
90 #define api_free_boolean(value)
91 #define api_free_integer(value)
92 #define api_free_float(value)
93 #define api_free_buffer(value)
94 #define api_free_window(value)
95 #define api_free_tabpage(value)
96 
101 typedef struct {
104  const struct msglist *const *msg_list;
105  int trylevel;
106  int got_int;
108  int did_emsg;
109 } TryState;
110 
111 // `msg_list` controls the collection of abort-causing non-exception errors,
112 // which would otherwise be ignored. This pattern is from do_cmdline().
113 //
114 // TODO(bfredl): prepare error-handling at "top level" (nv_event).
115 #define TRY_WRAP(code) \
116  do { \
117  struct msglist **saved_msg_list = msg_list; \
118  struct msglist *private_msg_list; \
119  msg_list = &private_msg_list; \
120  private_msg_list = NULL; \
121  code \
122  msg_list = saved_msg_list; /* Restore the exception context. */ \
123  } while (0)
124 
125 #ifdef INCLUDE_GENERATED_DECLARATIONS
126 # include "api/private/helpers.h.generated.h"
127 #endif
128 #endif // NVIM_API_PRIVATE_HELPERS_H
Definition: ex_eval.h:61
const struct msglist *const * msg_list
Definition: helpers.h:104
int need_rethrow
Definition: helpers.h:107
Definition: helpers.h:101
int did_emsg
Definition: helpers.h:108
int got_int
Definition: helpers.h:106
Definition: ex_eval.h:42
struct msglist * private_msg_list
Definition: helpers.h:103
except_T * current_exception
Definition: helpers.h:102
int trylevel
Definition: helpers.h:105