Macros | Functions | Variables
helpers.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "nvim/api/private/converter.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/decoration.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/extmark.h"
#include "nvim/fileio.h"
#include "nvim/getchar.h"
#include "nvim/lib/kvec.h"
#include "nvim/lua/executor.h"
#include "nvim/map.h"
#include "nvim/map_defs.h"
#include "nvim/mark.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/option.h"
#include "nvim/option_defs.h"
#include "nvim/syntax.h"
#include "nvim/ui.h"
#include "nvim/version.h"
#include "nvim/vim.h"
#include "nvim/window.h"

Macros

#define KEY_TO_BOOL(name)
 

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)
 
dictitem_Tdict_check_writable (dict_T *dict, String key, bool del, 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)
 
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)
 
Array string_to_array (const String input, bool crlf)
 
void modify_keymap (Buffer buffer, bool is_unmap, String mode, String lhs, String rhs, Dict(keymap) *opts, Error *err)
 
bool buf_collect_lines (buf_T *buf, size_t n, int64_t start, bool replace_nl, Array *l, 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)
 
bool extmark_get_index_from_obj (buf_T *buf, Integer ns_id, Object obj, int *row, colnr_T *col, Error *err)
 
VirtText parse_virt_text (Array chunks, Error *err, int *width)
 
bool api_object_to_bool (Object obj, const char *what, bool nil_value, Error *err)
 
int object_to_hl_id (Object obj, const char *what, Error *err)
 
HlMessage parse_hl_msg (Array chunks, Error *err)
 
bool api_dict_to_keydict (void *rv, field_hash hashy, Dictionary dict, Error *err)
 
void api_free_keydict (void *dict, KeySetLink *table)
 
bool set_mark (buf_T *buf, String name, Integer line, Integer col, Error *err)
 
const char * get_default_stl_hl (win_T *wp)
 Get default statusline highlight for window. More...
 

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

◆ KEY_TO_BOOL

#define KEY_TO_BOOL (   name)
Value:
parsed_args.name = api_object_to_bool(opts->name, #name, false, err); \
if (ERROR_SET(err)) { \
goto fail_and_free; \
}

Function Documentation

◆ api_clear_error()

void api_clear_error ( Error value)

◆ api_dict_to_keydict()

bool api_dict_to_keydict ( void rv,
field_hash  hashy,
Dictionary  dict,
Error err 
)

◆ api_free_array()

void api_free_array ( Array  value)

◆ api_free_dictionary()

void api_free_dictionary ( Dictionary  value)

◆ api_free_keydict()

void api_free_keydict ( void dict,
KeySetLink table 
)

◆ api_free_object()

void api_free_object ( Object  value)

◆ api_free_string()

void api_free_string ( String  value)

◆ api_metadata()

Dictionary api_metadata ( void  )

◆ api_object_to_bool()

bool api_object_to_bool ( Object  obj,
const char *  what,
bool  nil_value,
Error err 
)

Force obj to bool. If it fails, returns false and sets err

Parameters
objThe object to coerce to a boolean
whatThe name of the object, used for error message
nil_valueWhat to return if the type is nil.
errSet if there was an error in converting to a bool

◆ api_set_error()

void api_set_error ( Error err,
ErrorType  errType,
const char *  format,
  ... 
)

◆ ArrayOf()

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()

assert ( len >=  0)

◆ buf_collect_lines()

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

◆ cbuf_to_string()

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

◆ cchar_to_string()

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

◆ copy_array()

Array copy_array ( Array  array)

◆ copy_dictionary()

Dictionary copy_dictionary ( Dictionary  dict)

◆ copy_object()

Object copy_object ( Object  obj)

Creates a deep clone of an object.

◆ copy_string()

String copy_string ( String  str)

◆ cstr_as_string()

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

◆ cstr_to_string()

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

◆ cstrn_to_string()

String cstrn_to_string ( const char *  str,
size_t  maxsize 
)

◆ dict_check_writable()

dictitem_T* dict_check_writable ( dict_T dict,
String  key,
bool  del,
Error err 
)

◆ dict_get_value()

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

◆ dict_set_var()

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

◆ extmark_get_index_from_obj()

bool extmark_get_index_from_obj ( buf_T buf,
Integer  ns_id,
Object  obj,
int *  row,
colnr_T col,
Error err 
)

Gets the line and column of an extmark.

Extmarks may be queried by position, name or even special names in the future such as "cursor".

Parameters
[out]lnumextmark line
[out]colnrextmark column
Returns
true if the extmark was found, else false

◆ find_buffer_by_handle()

buf_T* find_buffer_by_handle ( Buffer  buffer,
Error err 
)

◆ find_tab_by_handle()

tabpage_T* find_tab_by_handle ( Tabpage  tabpage,
Error err 
)

◆ find_window_by_handle()

win_T* find_window_by_handle ( Window  window,
Error err 
)

◆ ga_take_string()

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.

◆ get_default_stl_hl()

const char* get_default_stl_hl ( win_T wp)

Get default statusline highlight for window.

◆ get_option_from()

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

◆ modify_keymap()

void modify_keymap ( Buffer  buffer,
bool  is_unmap,
String  mode,
String  lhs,
String  rhs,
Dict(keymap) *  opts,
Error err 
)

Set, tweak, or remove a mapping in a mode. Acts as the implementation for functions like nvim_buf_set_keymap.

Arguments are handled like nvim_set_keymap unless noted.

Parameters
bufferBuffer handle for a specific buffer, or 0 for the current buffer, or -1 to signify global behavior ("all buffers")
is_unmapWhen true, removes the mapping that matches {lhs}.

◆ object_to_hl_id()

int object_to_hl_id ( Object  obj,
const char *  what,
Error err 
)

◆ parse_hl_msg()

HlMessage parse_hl_msg ( Array  chunks,
Error err 
)

◆ parse_virt_text()

VirtText parse_virt_text ( Array  chunks,
Error err,
int *  width 
)

◆ set_mark()

bool set_mark ( buf_T buf,
String  name,
Integer  line,
Integer  col,
Error err 
)

Set a named mark buffer and mark name must be validated already

Parameters
bufferBuffer to set the mark on
nameMark name
lineLine number
colColumn/row number
Returns
true if the mark was set, else false

◆ set_option_to()

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

◆ string_to_array()

Array string_to_array ( const String  input,
bool  crlf 
)

Creates "readfile()-style" ArrayOf(String) from a binary string.

  • Lines break at
    (NL/LF/line-feed).
  • NUL bytes are replaced with NL.
  • If the last byte is a linebreak an extra empty list item is added.
Parameters
inputBinary string
crlfAlso break lines at CR and CRLF.
Returns
[allocated] String array

◆ try_end()

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

◆ try_enter()

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.

◆ try_leave()

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.

◆ try_start()

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()

va_copy ( args2  ,
args1   
)

◆ va_end() [1/2]

va_end ( args1  )

◆ va_end() [2/2]

va_end ( args2  )

◆ va_start()

va_start ( args1  ,
format   
)

◆ vsnprintf()

vsnprintf ( err->  msg,
bufsize  ,
format  ,
args2   
)

Variable Documentation

◆ args1

void va_list args1
Initial value:
{
assert(kErrorTypeNone != errType)

◆ args2

va_list args2

◆ bufsize

size_t bufsize = MIN((size_t)len + 1, 1024 * 1024)

◆ len

static int int len = vsnprintf(NULL, 0, format, args1)

◆ msg

err msg = xmalloc(bufsize)

◆ type

err type = errType
kErrorTypeNone
@ kErrorTypeNone
Definition: defs.h:27
assert
assert(len >=0)
api_object_to_bool
bool api_object_to_bool(Object obj, const char *what, bool nil_value, Error *err)
Definition: helpers.c:1205
name
char_u * name
Definition: userfunc.c:821
ERROR_SET
#define ERROR_SET(e)
Definition: defs.h:17