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/ex_cmds_defs.h"
#include "nvim/extmark.h"
#include "nvim/fileio.h"
#include "nvim/getchar.h"
#include "nvim/highlight_group.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/ui.h"
#include "nvim/version.h"
#include "nvim/vim.h"
#include "nvim/window.h"


#define KEY_TO_BOOL(name)
#define CMDLINE_APPEND_IF(cond, str)


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 (uint64_t channel_id, 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)
String buf_get_text (buf_T *buf, int64_t lnum, int64_t start_col, int64_t end_col, bool replace_nl, 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...
void create_user_command (String name, Object command, Dict(user_command) *opts, int flags, Error *err)
int find_sid (uint64_t channel_id)
sctx_T api_set_sctx (uint64_t channel_id)
int init_sign_text (char **sign_text, char *text)
bool string_iswhite (String str)
 Check if a string contains only whitespace characters. More...
void build_cmdline_str (char **cmdlinep, exarg_T *eap, CmdParseInfo *cmdinfo, char **args, size_t argc)


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 CMDLINE_APPEND_IF (   cond,
do { \
if (cond) { \
kv_concat(cmdline, str); \
} \
} while (0)


#define KEY_TO_BOOL (   name)
Value: = 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

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,

◆ api_set_sctx()

sctx_T api_set_sctx ( uint64_t  channel_id)

Sets sctx for API calls.

channel_idapi clients id. Used to determine if it's a internal call or a rpc call.
returns previous value of current_sctx. To be used to be used for restoring sctx to previous state.

◆ ArrayOf()

ArrayOf ( Dictionary  )

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

modeThe abbreviation for the mode
bufThe buffer to get the mapping array. NULL for global
from_luaWhether it is called from internal lua api.
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.

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
true unless err was set

◆ buf_get_text()

String buf_get_text ( buf_T buf,
int64_t  lnum,
int64_t  start_col,
int64_t  end_col,
bool  replace_nl,
Error err 

Returns a substring of a buffer line

bufBuffer handle
lnumLine number (1-based)
start_colStarting byte offset into line (0-based)
end_colEnding byte offset into line (0-based, exclusive)
replace_nlReplace newlines ('
') with null ('\0')
errError object
The text between start_col and end_col on line lnum of buffer buf

◆ build_cmdline_str()

void build_cmdline_str ( char **  cmdlinep,
exarg_T eap,
CmdParseInfo cmdinfo,
char **  args,
size_t  argc 

Build cmdline string for command, used by nvim_cmd().


◆ 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.

bufthe buffer to copy
sizelength of the buffer
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.

charthe char to convert
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)

◆ create_user_command()

void create_user_command ( String  name,
Object  command,
Dict(user_command) *  opts,
int  flags,
Error err 

◆ 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.

strthe C string to use
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.

strthe C string to copy
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

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.

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
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".

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

◆ find_buffer_by_handle()

buf_T* find_buffer_by_handle ( Buffer  buffer,
Error err 

◆ find_sid()

int find_sid ( uint64_t  channel_id)

◆ 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.

fromIf type is SREQ_WIN or SREQ_BUF, this must be a pointer to the window or buffer.
nameThe option name
[out]errDetails of an error that may have occurred
the option value

◆ init_sign_text()

int init_sign_text ( char **  sign_text,
char *  text 

◆ modify_keymap()

void modify_keymap ( uint64_t  channel_id,
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.

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

bufferBuffer to set the mark on
nameMark name
lineLine number
colColumn/row number
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.

toIf type is SREQ_WIN or SREQ_BUF, this must be a pointer to the window or buffer.
nameThe option name
[out]errDetails of an error that may have occurred

◆ string_iswhite()

bool string_iswhite ( String  str)

Check if a string contains only whitespace characters.

◆ string_to_array()

Array string_to_array ( const String  input,
bool  crlf 

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

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

◆ string_to_cstr()

char* string_to_cstr ( String  str)

Copies a String to an allocated, NUL-terminated C string.

strthe String to copy
the resulting C string

◆ try_end()

bool try_end ( Error err)

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

errPointer to the stack-allocated error object
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.

[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

Return is consistent with most functions (false on error), not with try_end (true on error).
[in]tstatePrevious state to restore.
[out]errLocation where error should be saved.
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  ,

◆ va_end() [1/2]

va_end ( args1  )

◆ va_end() [2/2]

va_end ( args2  )

◆ va_start()

va_start ( args1  ,

◆ vsnprintf()

vsnprintf ( err->  msg,
bufsize  ,
format  ,

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
Definition: defs.h:27
assert(len >=0)
bool api_object_to_bool(Object obj, const char *what, bool nil_value, Error *err)
Definition: helpers.c:1291
char_u * name
Definition: userfunc.c:817
#define ERROR_SET(e)
Definition: defs.h:17