From 18903bd9b88ec960cb36b1ddd2b5062aad4bac2e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 31 Jan 2016 00:06:46 +0300 Subject: eval: Add special variable type --- src/nvim/api/private/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 7a0b5191d7..c770618ce4 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -397,13 +397,13 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err) switch (obj.type) { case kObjectTypeNil: - tv->v_type = VAR_NUMBER; - tv->vval.v_number = 0; + tv->v_type = VAR_SPECIAL; + tv->vval.v_special = kSpecialVarNull; break; case kObjectTypeBoolean: - tv->v_type = VAR_NUMBER; - tv->vval.v_number = obj.data.boolean; + tv->v_type = VAR_SPECIAL; + tv->vval.v_special = obj.data.boolean? kSpecialVarTrue: kSpecialVarFalse; break; case kObjectTypeBuffer: -- cgit From d70a322c40e849f98ad573d2a37dc680c5616b26 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 31 Jan 2016 01:25:00 +0300 Subject: eval: Add special variables v:false, v:null, v:none --- src/nvim/api/private/helpers.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/nvim/api') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index c770618ce4..a8082655fd 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -651,6 +651,22 @@ static Object vim_to_object_rec(typval_T *obj, PMap(ptr_t) *lookup) } switch (obj->v_type) { + case VAR_SPECIAL: + switch (obj->vval.v_special) { + case kSpecialVarTrue: + case kSpecialVarFalse: { + rv.type = kObjectTypeBoolean; + rv.data.boolean = (obj->vval.v_special == kSpecialVarTrue); + break; + } + case kSpecialVarNull: + case kSpecialVarNone: { + rv.type = kObjectTypeNil; + break; + } + } + break; + case VAR_STRING: rv.type = kObjectTypeString; rv.data.string = cstr_to_string((char *) obj->vval.v_string); @@ -730,6 +746,10 @@ static Object vim_to_object_rec(typval_T *obj, PMap(ptr_t) *lookup) } } break; + + case VAR_UNKNOWN: + case VAR_FUNC: + break; } return rv; -- cgit From 6167ce6df2753d5474ad49aea19f5957128ab015 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 6 Feb 2016 02:46:23 +0300 Subject: eval: Remove v:none To get v:none back just rever this commit. This will not make json*() functions compatible with Vim though. --- src/nvim/api/private/helpers.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index a8082655fd..9082dfd759 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -659,8 +659,7 @@ static Object vim_to_object_rec(typval_T *obj, PMap(ptr_t) *lookup) rv.data.boolean = (obj->vval.v_special == kSpecialVarTrue); break; } - case kSpecialVarNull: - case kSpecialVarNone: { + case kSpecialVarNull: { rv.type = kObjectTypeNil; break; } -- cgit From f1ced96c28b7db7b3dad9b0ca2f71f8d046ef732 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 11 Feb 2016 02:01:17 +0300 Subject: api: Replace set_var(name, NIL) with del_var(name) --- src/nvim/api/buffer.c | 21 +++++++++++++++++++-- src/nvim/api/private/helpers.c | 9 ++++++--- src/nvim/api/tabpage.c | 21 +++++++++++++++++++-- src/nvim/api/vim.c | 14 ++++++++++++-- src/nvim/api/window.c | 21 +++++++++++++++++++-- 5 files changed, 75 insertions(+), 11 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index c25a9789c5..075f101f61 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -423,7 +423,7 @@ Object buffer_get_var(Buffer buffer, String name, Error *err) return dict_get_value(buf->b_vars, name, err); } -/// Sets a buffer-scoped (b:) variable. 'nil' value deletes the variable. +/// Sets a buffer-scoped (b:) variable /// /// @param buffer The buffer handle /// @param name The variable name @@ -438,7 +438,24 @@ Object buffer_set_var(Buffer buffer, String name, Object value, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(buf->b_vars, name, value, err); + return dict_set_value(buf->b_vars, name, value, false, err); +} + +/// Removes a buffer-scoped (b:) variable +/// +/// @param buffer The buffer handle +/// @param name The variable name +/// @param[out] err Details of an error that may have occurred +/// @return The old value +Object buffer_del_var(Buffer buffer, String name, Error *err) +{ + buf_T *buf = find_buffer_by_handle(buffer, err); + + if (!buf) { + return (Object) OBJECT_INIT; + } + + return dict_set_value(buf->b_vars, name, NIL, true, err); } /// Gets a buffer option value diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 9082dfd759..db3e499427 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -90,14 +90,17 @@ Object dict_get_value(dict_T *dict, String key, Error *err) } /// Set a value in a dict. Objects are recursively expanded into their -/// vimscript equivalents. Passing 'nil' as value deletes the key. +/// vimscript equivalents. /// /// @param dict The vimscript dict /// @param key The key /// @param value The new value +/// @param del Delete key in place of setting it. Argument `value` is ignored in +/// this case. /// @param[out] err Details of an error that may have occurred /// @return the old value, if any -Object dict_set_value(dict_T *dict, String key, Object value, Error *err) +Object dict_set_value(dict_T *dict, String key, Object value, bool del, + Error *err) { Object rv = OBJECT_INIT; @@ -118,7 +121,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, Error *err) dictitem_T *di = dict_find(dict, (uint8_t *)key.data, (int)key.size); - if (value.type == kObjectTypeNil) { + if (del) { // Delete the key if (di == NULL) { // Doesn't exist, fail diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c index 126ee4072d..475b75b571 100644 --- a/src/nvim/api/tabpage.c +++ b/src/nvim/api/tabpage.c @@ -54,7 +54,7 @@ Object tabpage_get_var(Tabpage tabpage, String name, Error *err) return dict_get_value(tab->tp_vars, name, err); } -/// Sets a tab-scoped (t:) variable. 'nil' value deletes the variable. +/// Sets a tab-scoped (t:) variable /// /// @param tabpage handle /// @param name The variable name @@ -69,7 +69,24 @@ Object tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(tab->tp_vars, name, value, err); + return dict_set_value(tab->tp_vars, name, value, false, err); +} + +/// Removes a tab-scoped (t:) variable +/// +/// @param tabpage handle +/// @param name The variable name +/// @param[out] err Details of an error that may have occurred +/// @return The tab page handle +Object tabpage_del_var(Tabpage tabpage, String name, Error *err) +{ + tabpage_T *tab = find_tab_by_handle(tabpage, err); + + if (!tab) { + return (Object) OBJECT_INIT; + } + + return dict_set_value(tab->tp_vars, name, NIL, true, err); } /// Gets the current window in a tab page diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 9279f6b469..09d5f1b6f8 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -331,7 +331,7 @@ Object vim_get_var(String name, Error *err) return dict_get_value(&globvardict, name, err); } -/// Sets a global variable. Passing 'nil' as value deletes the variable. +/// Sets a global variable /// /// @param name The variable name /// @param value The variable value @@ -339,7 +339,17 @@ Object vim_get_var(String name, Error *err) /// @return the old value if any Object vim_set_var(String name, Object value, Error *err) { - return dict_set_value(&globvardict, name, value, err); + return dict_set_value(&globvardict, name, value, false, err); +} + +/// Removes a global variable +/// +/// @param name The variable name +/// @param[out] err Details of an error that may have occurred +/// @return the old value if any +Object vim_del_var(String name, Error *err) +{ + return dict_set_value(&globvardict, name, NIL, true, err); } /// Gets a vim variable diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index aad616c7bf..58218af09d 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -197,7 +197,7 @@ Object window_get_var(Window window, String name, Error *err) return dict_get_value(win->w_vars, name, err); } -/// Sets a window-scoped (w:) variable. 'nil' value deletes the variable. +/// Sets a window-scoped (w:) variable /// /// @param window The window handle /// @param name The variable name @@ -212,7 +212,24 @@ Object window_set_var(Window window, String name, Object value, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(win->w_vars, name, value, err); + return dict_set_value(win->w_vars, name, value, false, err); +} + +/// Removes a window-scoped (w:) variable +/// +/// @param window The window handle +/// @param name The variable name +/// @param[out] err Details of an error that may have occurred +/// @return The old value +Object window_del_var(Window window, String name, Error *err) +{ + win_T *win = find_window_by_handle(window, err); + + if (!win) { + return (Object) OBJECT_INIT; + } + + return dict_set_value(win->w_vars, name, NIL, true, err); } /// Gets a window option value -- cgit From 7cdd01983aeb452e0a3f3eb027e75fe02ce48718 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 21 Feb 2016 05:17:20 +0300 Subject: api/documentation: Add a warning that nil may mean v:null --- src/nvim/api/buffer.c | 5 ++++- src/nvim/api/tabpage.c | 5 ++++- src/nvim/api/vim.c | 5 ++++- src/nvim/api/window.c | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 075f101f61..a01188f98c 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -429,7 +429,10 @@ Object buffer_get_var(Buffer buffer, String name, Error *err) /// @param name The variable name /// @param value The variable value /// @param[out] err Details of an error that may have occurred -/// @return The old value +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object buffer_set_var(Buffer buffer, String name, Object value, Error *err) { buf_T *buf = find_buffer_by_handle(buffer, err); diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c index 475b75b571..3148a4fab7 100644 --- a/src/nvim/api/tabpage.c +++ b/src/nvim/api/tabpage.c @@ -60,7 +60,10 @@ Object tabpage_get_var(Tabpage tabpage, String name, Error *err) /// @param name The variable name /// @param value The variable value /// @param[out] err Details of an error that may have occurred -/// @return The tab page handle +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err) { tabpage_T *tab = find_tab_by_handle(tabpage, err); diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 09d5f1b6f8..dcae08d24c 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -336,7 +336,10 @@ Object vim_get_var(String name, Error *err) /// @param name The variable name /// @param value The variable value /// @param[out] err Details of an error that may have occurred -/// @return the old value if any +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object vim_set_var(String name, Object value, Error *err) { return dict_set_value(&globvardict, name, value, false, err); diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 58218af09d..cca1e3bdd3 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -203,7 +203,10 @@ Object window_get_var(Window window, String name, Error *err) /// @param name The variable name /// @param value The variable value /// @param[out] err Details of an error that may have occurred -/// @return The old value +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object window_set_var(Window window, String name, Object value, Error *err) { win_T *win = find_window_by_handle(window, err); -- cgit From 25bb08dad930a975010798c592b5277d1b5a18b0 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 17 Mar 2016 23:31:31 +0300 Subject: api: Add warnings about the returned value to \*del_var functions --- src/nvim/api/buffer.c | 5 ++++- src/nvim/api/private/defs.h | 1 - src/nvim/api/tabpage.c | 5 ++++- src/nvim/api/vim.c | 5 ++++- src/nvim/api/window.c | 5 ++++- 5 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index a01188f98c..55b535c78c 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -449,7 +449,10 @@ Object buffer_set_var(Buffer buffer, String name, Object value, Error *err) /// @param buffer The buffer handle /// @param name The variable name /// @param[out] err Details of an error that may have occurred -/// @return The old value +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object buffer_del_var(Buffer buffer, String name, Error *err) { buf_T *buf = find_buffer_by_handle(buffer, err); diff --git a/src/nvim/api/private/defs.h b/src/nvim/api/private/defs.h index 6c8e324649..fbfa87d5ae 100644 --- a/src/nvim/api/private/defs.h +++ b/src/nvim/api/private/defs.h @@ -99,4 +99,3 @@ struct key_value_pair { #endif // NVIM_API_PRIVATE_DEFS_H - diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c index 3148a4fab7..c8311b0aa0 100644 --- a/src/nvim/api/tabpage.c +++ b/src/nvim/api/tabpage.c @@ -80,7 +80,10 @@ Object tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err) /// @param tabpage handle /// @param name The variable name /// @param[out] err Details of an error that may have occurred -/// @return The tab page handle +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object tabpage_del_var(Tabpage tabpage, String name, Error *err) { tabpage_T *tab = find_tab_by_handle(tabpage, err); diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index dcae08d24c..10110b0f62 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -349,7 +349,10 @@ Object vim_set_var(String name, Object value, Error *err) /// /// @param name The variable name /// @param[out] err Details of an error that may have occurred -/// @return the old value if any +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object vim_del_var(String name, Error *err) { return dict_set_value(&globvardict, name, NIL, true, err); diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index cca1e3bdd3..a52f53a3e6 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -223,7 +223,10 @@ Object window_set_var(Window window, String name, Object value, Error *err) /// @param window The window handle /// @param name The variable name /// @param[out] err Details of an error that may have occurred -/// @return The old value +/// @return The old value or nil if there was no previous value. +/// +/// @warning It may return nil if there was no previous value +/// or if previous value was `v:null`. Object window_del_var(Window window, String name, Error *err) { win_T *win = find_window_by_handle(window, err); -- cgit