1 #ifndef NVIM_API_PRIVATE_HELPERS_H 2 #define NVIM_API_PRIVATE_HELPERS_H 3 4 #include "nvim/api/private/defs.h" 5 #include "nvim/decoration.h" 6 #include "nvim/ex_eval.h" 7 #include "nvim/getchar.h" 8 #include "nvim/lib/kvec.h" 9 #include "nvim/memory.h" 10 #include "nvim/vim.h" 11 12 #define OBJECT_OBJ(o) o 13 14 #define BOOLEAN_OBJ(b) ((Object) { \ 15 .type = kObjectTypeBoolean, \ 16 .data.boolean = b }) 17 #define BOOL(b) BOOLEAN_OBJ(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 CSTR_TO_OBJ(s) STRING_OBJ(cstr_to_string(s)) 32 33 #define BUFFER_OBJ(s) ((Object) { \ 34 .type = kObjectTypeBuffer, \ 35 .data.integer = s }) 36 37 #define WINDOW_OBJ(s) ((Object) { \ 38 .type = kObjectTypeWindow, \ 39 .data.integer = s }) 40 41 #define TABPAGE_OBJ(s) ((Object) { \ 42 .type = kObjectTypeTabpage, \ 43 .data.integer = s }) 44 45 #define ARRAY_OBJ(a) ((Object) { \ 46 .type = kObjectTypeArray, \ 47 .data.array = a }) 48 49 #define DICTIONARY_OBJ(d) ((Object) { \ 50 .type = kObjectTypeDictionary, \ 51 .data.dictionary = d }) 52 53 #define LUAREF_OBJ(r) ((Object) { \ 54 .type = kObjectTypeLuaRef, \ 55 .data.luaref = r }) 56 57 #define NIL ((Object)OBJECT_INIT) 58 #define NULL_STRING ((String)STRING_INIT) 59 60 // currently treat key=vim.NIL as if the key was missing 61 #define HAS_KEY(o) ((o).type != kObjectTypeNil) 62 63 #define PUT(dict, k, v) \ 64 kv_push(dict, ((KeyValuePair) { .key = cstr_to_string(k), .value = v })) 65 66 #define PUT_BOOL(dict, name, condition) PUT(dict, name, BOOLEAN_OBJ(condition)); 67 68 #define ADD(array, item) \ 69 kv_push(array, item) 70 71 #define FIXED_TEMP_ARRAY(name, fixsize) \ 72 Array name = ARRAY_DICT_INIT; \ 73 Object name##__items[fixsize]; \ 74 name.size = fixsize; \ 75 name.items = name##__items; \ 76 77 #define STATIC_CSTR_AS_STRING(s) ((String) { .data = s, .size = sizeof(s) - 1 }) 78 79 /// Create a new String instance, putting data in allocated memory 80 /// 81 /// @param[in] s String to work with. Must be a string literal. 82 #define STATIC_CSTR_TO_STRING(s) ((String){ \ 83 .data = xmemdupz(s, sizeof(s) - 1), \ 84 .size = sizeof(s) - 1 }) 85 86 // Helpers used by the generated msgpack-rpc api wrappers 87 #define api_init_boolean 88 #define api_init_integer 89 #define api_init_float 90 #define api_init_string = STRING_INIT 91 #define api_init_buffer 92 #define api_init_window 93 #define api_init_tabpage 94 #define api_init_object = NIL 95 #define api_init_array = ARRAY_DICT_INIT 96 #define api_init_dictionary = ARRAY_DICT_INIT 97 98 #define api_free_boolean(value) 99 #define api_free_integer(value) 100 #define api_free_float(value) 101 #define api_free_buffer(value) 102 #define api_free_window(value) 103 #define api_free_tabpage(value) 104 105 EXTERN PMap(handle_T) buffer_handles INIT(= MAP_INIT); 106 EXTERN PMap(handle_T) window_handles INIT(= MAP_INIT); 107 EXTERN PMap(handle_T) tabpage_handles INIT(= MAP_INIT); 108 109 #define handle_get_buffer(h) pmap_get(handle_T)(&buffer_handles, (h)) 110 #define handle_get_window(h) pmap_get(handle_T)(&window_handles, (h)) 111 #define handle_get_tabpage(h) pmap_get(handle_T)(&tabpage_handles, (h)) 112 113 /// Structure used for saving state for :try 114 /// 115 /// Used when caller is supposed to be operating when other VimL code is being 116 /// processed and that “other VimL code” must not be affected. 117 typedef struct { 118 except_T *current_exception; 119 struct msglist *private_msg_list; 120 const struct msglist *const *msg_list; 121 int trylevel; 122 int got_int; 123 int need_rethrow; 124 int did_emsg; 125 } TryState; 126 127 // `msg_list` controls the collection of abort-causing non-exception errors, 128 // which would otherwise be ignored. This pattern is from do_cmdline(). 129 // 130 // TODO(bfredl): prepare error-handling at "top level" (nv_event). 131 #define TRY_WRAP(code) \ 132 do { \ 133 struct msglist **saved_msg_list = msg_list; \ 134 struct msglist *private_msg_list; \ 135 msg_list = &private_msg_list; \ 136 private_msg_list = NULL; \ 137 code \ 138 msg_list = saved_msg_list; /* Restore the exception context. */ \ 139 } while (0) 140 141 #ifdef INCLUDE_GENERATED_DECLARATIONS 142 # include "api/private/helpers.h.generated.h" 143 # include "keysets.h.generated.h" 144 #endif 145 146 147 #endif // NVIM_API_PRIVATE_HELPERS_H 148