Data Structures | Macros | Functions | Variables
helpers.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "nvim/api/private/helpers.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/handle.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/ascii.h"
#include "nvim/vim.h"
#include "nvim/buffer.h"
#include "nvim/window.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/map_defs.h"
#include "nvim/map.h"
#include "nvim/option.h"
#include "nvim/option_defs.h"
#include "nvim/version.h"
#include "nvim/lib/kvec.h"
#include "nvim/getchar.h"
#include "nvim/fileio.h"
#include "nvim/ui.h"
#include "nvim/eval/typval_encode.c.h"

Data Structures

struct  EncodedData
 Helper structure for vim_to_object. More...
 

Macros

#define TYPVAL_ENCODE_ALLOW_SPECIALS   false
 
#define TYPVAL_ENCODE_CONV_NIL(tv)   kv_push(edata->stack, NIL)
 
#define TYPVAL_ENCODE_CONV_BOOL(tv, num)   kv_push(edata->stack, BOOLEAN_OBJ((Boolean)(num)))
 
#define TYPVAL_ENCODE_CONV_NUMBER(tv, num)   kv_push(edata->stack, INTEGER_OBJ((Integer)(num)))
 
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER   TYPVAL_ENCODE_CONV_NUMBER
 
#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt)   kv_push(edata->stack, FLOAT_OBJ((Float)(flt)))
 
#define TYPVAL_ENCODE_CONV_STRING(tv, str, len)
 
#define TYPVAL_ENCODE_CONV_STR_STRING   TYPVAL_ENCODE_CONV_STRING
 
#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, str, len, type)   TYPVAL_ENCODE_CONV_NIL(tv)
 
#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun)
 
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS(tv, len)
 
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF(tv, len)
 
#define TYPVAL_ENCODE_CONV_FUNC_END(tv)
 
#define TYPVAL_ENCODE_CONV_EMPTY_LIST(tv)   kv_push(edata->stack, ARRAY_OBJ(((Array) { .capacity = 0, .size = 0 })))
 
#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict)
 
#define TYPVAL_ENCODE_CONV_LIST_START(tv, len)   typval_encode_list_start(edata, (size_t)(len))
 
#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv)
 
#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv)   typval_encode_between_list_items(edata)
 
#define TYPVAL_ENCODE_CONV_LIST_END(tv)   typval_encode_list_end(edata)
 
#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len)   typval_encode_dict_start(edata, (size_t)(len))
 
#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv)
 
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, kv_pair)
 
#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict)   typval_encode_after_key(edata)
 
#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)   typval_encode_between_dict_items(edata)
 
#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict)   typval_encode_dict_end(edata)
 
#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type)   TYPVAL_ENCODE_CONV_NIL(val)
 
#define TYPVAL_ENCODE_SCOPE   static
 
#define TYPVAL_ENCODE_NAME   object
 
#define TYPVAL_ENCODE_FIRST_ARG_TYPE   EncodedData *const
 
#define TYPVAL_ENCODE_FIRST_ARG_NAME   edata
 

Functions

void try_enter (TryState *const tstate)
 
bool try_leave (const TryState *const tstate, Error *const err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
void try_start (void)
 
bool try_end (Error *err)
 
Object dict_get_value (dict_T *dict, String key, Error *err)
 
Object dict_set_var (dict_T *dict, String key, Object value, bool del, bool retval, Error *err)
 
Object get_option_from (void *from, int type, String name, Error *err)
 
void set_option_to (uint64_t channel_id, void *to, int type, String name, Object value, Error *err)
 
Object vim_to_object (typval_T *obj)
 
buf_Tfind_buffer_by_handle (Buffer buffer, Error *err)
 
win_Tfind_window_by_handle (Window window, Error *err)
 
tabpage_Tfind_tab_by_handle (Tabpage tabpage, Error *err)
 
String cchar_to_string (char c)
 
String cstr_to_string (const char *str)
 
String cbuf_to_string (const char *buf, size_t size) FUNC_ATTR_NONNULL_ALL
 
String cstrn_to_string (const char *str, size_t maxsize) FUNC_ATTR_NONNULL_ALL
 
String cstr_as_string (char *str) FUNC_ATTR_PURE
 
String ga_take_string (garray_T *ga)
 
bool buf_collect_lines (buf_T *buf, size_t n, int64_t start, bool replace_nl, Array *l, Error *err)
 
bool object_to_vim (Object obj, typval_T *tv, Error *err)
 
void api_free_string (String value)
 
void api_free_object (Object value)
 
void api_free_array (Array value)
 
void api_free_dictionary (Dictionary value)
 
void api_clear_error (Error *value) FUNC_ATTR_NONNULL_ALL
 
Dictionary api_metadata (void)
 
String copy_string (String str)
 
Array copy_array (Array array)
 
Dictionary copy_dictionary (Dictionary dict)
 
Object copy_object (Object obj)
 Creates a deep clone of an object. More...
 
void api_set_error (Error *err, ErrorType errType, const char *format,...) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PRINTF(3
 
 va_start (args1, format)
 
 va_copy (args2, args1)
 
 va_end (args1)
 
 assert (len >=0)
 
 vsnprintf (err->msg, bufsize, format, args2)
 
 va_end (args2)
 
 ArrayOf (Dictionary)
 

Variables

void va_list args1
 
va_list args2
 
int len = vsnprintf(NULL, 0, format, args1)
 
size_t bufsize = MIN((size_t)len + 1, 1024 * 1024)
 
err msg = xmalloc(bufsize)
 
err type = errType
 

Macro Definition Documentation

#define TYPVAL_ENCODE_ALLOW_SPECIALS   false
#define TYPVAL_ENCODE_CONV_BOOL (   tv,
  num 
)    kv_push(edata->stack, BOOLEAN_OBJ((Boolean)(num)))
#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY (   tv,
  dict 
)    typval_encode_after_key(edata)
#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS (   tv,
  dict 
)    typval_encode_between_dict_items(edata)
#define TYPVAL_ENCODE_CONV_DICT_END (   tv,
  dict 
)    typval_encode_dict_end(edata)
#define TYPVAL_ENCODE_CONV_DICT_START (   tv,
  dict,
  len 
)    typval_encode_dict_start(edata, (size_t)(len))
#define TYPVAL_ENCODE_CONV_EMPTY_DICT (   tv,
  dict 
)
Value:
kv_push(edata->stack, \
DICTIONARY_OBJ(((Dictionary) { .capacity = 0, .size = 0 })))
#define kv_push(v, x)
Definition: kvec.h:97
Definition: defs.h:95
#define DICTIONARY_OBJ(d)
Definition: helpers.h:46
#define TYPVAL_ENCODE_CONV_EMPTY_LIST (   tv)    kv_push(edata->stack, ARRAY_OBJ(((Array) { .capacity = 0, .size = 0 })))
#define TYPVAL_ENCODE_CONV_EXT_STRING (   tv,
  str,
  len,
  type 
)    TYPVAL_ENCODE_CONV_NIL(tv)
#define TYPVAL_ENCODE_CONV_FLOAT (   tv,
  flt 
)    kv_push(edata->stack, FLOAT_OBJ((Float)(flt)))
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS (   tv,
  len 
)
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF (   tv,
  len 
)
#define TYPVAL_ENCODE_CONV_FUNC_END (   tv)
#define TYPVAL_ENCODE_CONV_FUNC_START (   tv,
  fun 
)
Value:
do { \
goto typval_encode_stop_converting_one_item; \
} while (0)
typval_T tv
Definition: typval.c:1539
#define TYPVAL_ENCODE_CONV_NIL(tv)
Definition: helpers.c:405
#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS (   tv)    typval_encode_between_list_items(edata)
#define TYPVAL_ENCODE_CONV_LIST_END (   tv)    typval_encode_list_end(edata)
#define TYPVAL_ENCODE_CONV_LIST_START (   tv,
  len 
)    typval_encode_list_start(edata, (size_t)(len))
#define TYPVAL_ENCODE_CONV_NIL (   tv)    kv_push(edata->stack, NIL)
#define TYPVAL_ENCODE_CONV_NUMBER (   tv,
  num 
)    kv_push(edata->stack, INTEGER_OBJ((Integer)(num)))
#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START (   tv,
  dict,
  mpsv 
)
#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START (   tv,
  mpsv 
)
#define TYPVAL_ENCODE_CONV_RECURSE (   val,
  conv_type 
)    TYPVAL_ENCODE_CONV_NIL(val)
#define TYPVAL_ENCODE_CONV_STR_STRING   TYPVAL_ENCODE_CONV_STRING
#define TYPVAL_ENCODE_CONV_STRING (   tv,
  str,
  len 
)
Value:
do { \
const size_t len_ = (size_t)(len); \
const char *const str_ = (const char *)(str); \
assert(len_ == 0 || str_ != NULL); \
kv_push(edata->stack, STRING_OBJ(((String) { \
.data = xmemdupz((len_?str_:""), len_), \
.size = len_ \
}))); \
} while (0)
Definition: defs.h:77
return NULL
Definition: eval.c:22229
int len
Definition: helpers.c:1181
void * xmemdupz(const void *data, size_t len) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
Definition: memory.c:192
#define kv_push(v, x)
Definition: kvec.h:97
assert(len >=0)
#define STRING_OBJ(s)
Definition: helpers.h:26
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER   TYPVAL_ENCODE_CONV_NUMBER
#define TYPVAL_ENCODE_FIRST_ARG_NAME   edata
#define TYPVAL_ENCODE_FIRST_ARG_TYPE   EncodedData *const
#define TYPVAL_ENCODE_NAME   object
#define TYPVAL_ENCODE_SCOPE   static
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK (   label,
  kv_pair 
)

Function Documentation

void api_clear_error ( Error value)
void api_free_array ( Array  value)
void api_free_dictionary ( Dictionary  value)
void api_free_object ( Object  value)
void api_free_string ( String  value)
Dictionary api_metadata ( void  )
void api_set_error ( Error err,
ErrorType  errType,
const char *  format,
  ... 
)
ArrayOf ( Dictionary  )

Get an array containing dictionaries describing mappings based on mode and buffer id

Parameters
modeThe abbreviation for the mode
bufThe buffer to get the mapping array. NULL for global
Returns
Array of maparg()-like dictionaries describing mappings
assert ( len >=  0)
bool buf_collect_lines ( buf_T buf,
size_t  n,
int64_t  start,
bool  replace_nl,
Array l,
Error err 
)

Collects n buffer lines into array l, optionally replacing newlines with NUL.

Parameters
bufBuffer to get lines from
nNumber of lines to collect
replace_nlReplace newlines ("\n") with NUL
startLine number to start from
[out]lLines are copied here
err[out]Error, if any
Returns
true unless err was set
String cbuf_to_string ( const char *  buf,
size_t  size 
)

Copies buffer to an allocated String. The resulting string is also NUL-terminated, to facilitate interoperating with code using C strings.

Parameters
bufthe buffer to copy
sizelength of the buffer
Returns
the resulting String, if the input string was NULL, an empty String is returned
String cchar_to_string ( char  c)

Allocates a String consisting of a single char. Does not support multibyte characters. The resulting string is also NUL-terminated, to facilitate interoperating with code using C strings.

Parameters
charthe char to convert
Returns
the resulting String, if the input char was NUL, an empty String is returned
Array copy_array ( Array  array)
Dictionary copy_dictionary ( Dictionary  dict)
Object copy_object ( Object  obj)

Creates a deep clone of an object.

String copy_string ( String  str)
String cstr_as_string ( char *  str)

Creates a String using the given C string. Unlike cstr_to_string this function DOES NOT copy the C string.

Parameters
strthe C string to use
Returns
The resulting String, or an empty String if str was NULL
String cstr_to_string ( const char *  str)

Copies a C string into a String (binary safe string, characters + length). The resulting string is also NUL-terminated, to facilitate interoperating with code using C strings.

Parameters
strthe C string to copy
Returns
the resulting String, if the input string was NULL, an empty String is returned
String cstrn_to_string ( const char *  str,
size_t  maxsize 
)
Object dict_get_value ( dict_T dict,
String  key,
Error err 
)

Recursively expands a vimscript value in a dict

Parameters
dictThe vimscript dict
keyThe key
[out]errDetails of an error that may have occurred
Object dict_set_var ( dict_T dict,
String  key,
Object  value,
bool  del,
bool  retval,
Error err 
)

Set a value in a scope dict. Objects are recursively expanded into their vimscript equivalents.

Parameters
dictThe vimscript dict
keyThe key
valueThe new value
delDelete key in place of setting it. Argument value is ignored in this case.
retvalIf true the old value will be converted and returned.
[out]errDetails of an error that may have occurred
Returns
The old value if retval is true and the key was present, else NIL
buf_T* find_buffer_by_handle ( Buffer  buffer,
Error err 
)
tabpage_T* find_tab_by_handle ( Tabpage  tabpage,
Error err 
)
win_T* find_window_by_handle ( Window  window,
Error err 
)
String ga_take_string ( garray_T ga)

Return the owned memory of a ga as a String

Reinitializes the ga to a valid empty state.

Object get_option_from ( void *  from,
int  type,
String  name,
Error err 
)

Gets the value of a global or local(buffer, window) option.

Parameters
fromIf type is SREQ_WIN or SREQ_BUF, this must be a pointer to the window or buffer.
typeOne of SREQ_GLOBAL, SREQ_WIN or SREQ_BUF
nameThe option name
[out]errDetails of an error that may have occurred
Returns
the option value
bool object_to_vim ( Object  obj,
typval_T tv,
Error err 
)

Converts from type Object to a VimL value.

Parameters
objObject to convert from.
tvConversion result is placed here. On failure member v_type is set to VAR_UNKNOWN (no allocation was made for this variable). returns true if conversion is successful, otherwise false.
void set_option_to ( uint64_t  channel_id,
void *  to,
int  type,
String  name,
Object  value,
Error err 
)

Sets the value of a global or local(buffer, window) option.

Parameters
toIf type is SREQ_WIN or SREQ_BUF, this must be a pointer to the window or buffer.
typeOne of SREQ_GLOBAL, SREQ_WIN or SREQ_BUF
nameThe option name
[out]errDetails of an error that may have occurred
bool try_end ( Error err)

End try block, set the error message if any and return true if an error occurred.

Parameters
errPointer to the stack-allocated error object
Returns
true if an error occurred
void try_enter ( TryState *const  tstate)

Start block that may cause VimL exceptions while evaluating another code

Used when caller is supposed to be operating when other VimL code is being processed and that “other VimL code” must not be affected.

Parameters
[out]tstateLocation where try state should be saved.
bool try_leave ( const TryState *const  tstate,
Error *const  err 
)

End try block, set the error message if any and restore previous state

Warning
Return is consistent with most functions (false on error), not with try_end (true on error).
Parameters
[in]tstatePrevious state to restore.
[out]errLocation where error should be saved.
Returns
false if error occurred, true otherwise.
void try_start ( void  )

Start block that may cause vimscript exceptions

Each try_start() call should be mirrored by try_end() call.

To be used as a replacement of :try … catch … endtry in C code, in cases when error flag could not already be set. If there may be pending error state at the time try_start() is executed which needs to be preserved, try_enter()/try_leave() pair should be used instead.

va_copy ( args2  ,
args1   
)
va_end ( args1  )
va_end ( args2  )
va_start ( args1  ,
format   
)
Object vim_to_object ( typval_T obj)

Convert a vim object to an Object instance, recursively expanding Arrays/Dictionaries.

Parameters
objThe source object
Returns
The converted value
vsnprintf ( err->  msg,
bufsize  ,
format  ,
args2   
)

Variable Documentation

void va_list args1
Initial value:
{
assert(kErrorTypeNone != errType)
assert(len >=0)
Definition: defs.h:26
va_list args2
size_t bufsize = MIN((size_t)len + 1, 1024 * 1024)
size_t len = vsnprintf(NULL, 0, format, args1)
err msg = xmalloc(bufsize)
err type = errType