From 0eab7ac4b98d9a29e9713251a0402b5fbc37355e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 30 Jul 2016 01:39:09 +0300 Subject: api/buffer: Add nvim_buf_get_changedtick method --- src/nvim/api/buffer.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 738a5ae091..f286306a20 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -411,10 +411,13 @@ end: /// Gets a buffer-scoped (b:) variable. /// -/// @param buffer Buffer handle -/// @param name Variable name -/// @param[out] err Error details, if any -/// @return Variable value +/// @note Use nvim_buf_get_changedtick() for “variable” `b:changedtick`. +/// This method only gets variables from b: dictionary. +/// +/// @param buffer The buffer handle +/// @param name The variable name +/// @param[out] err Details of an error that may have occurred +/// @return The variable value Object nvim_buf_get_var(Buffer buffer, String name, Error *err) { buf_T *buf = find_buffer_by_handle(buffer, err); @@ -426,6 +429,22 @@ Object nvim_buf_get_var(Buffer buffer, String name, Error *err) return dict_get_value(buf->b_vars, name, err); } +/// Gets a changed tick of a buffer +/// +/// @param[in] buffer The buffer handle. +/// +/// @return `b:changedtick` value. +Integer nvim_buf_get_changedtick(Buffer buffer, Error *err) +{ + const buf_T *const buf = find_buffer_by_handle(buffer, err); + + if (!buf) { + return -1; + } + + return buf->b_changedtick; +} + /// Sets a buffer-scoped (b:) variable /// /// @param buffer Buffer handle -- cgit From 4f10d42f829da51cd79ff756ac4db96a6532dbac Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 15 Feb 2017 02:00:45 +0300 Subject: buffer: Bind b:changedtick to b:['changedtick'], remove special cases --- src/nvim/api/buffer.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index f286306a20..70246930bf 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -411,9 +411,6 @@ end: /// Gets a buffer-scoped (b:) variable. /// -/// @note Use nvim_buf_get_changedtick() for “variable” `b:changedtick`. -/// This method only gets variables from b: dictionary. -/// /// @param buffer The buffer handle /// @param name The variable name /// @param[out] err Details of an error that may have occurred -- cgit From 8faa4af39673ac032362b62662eb049f588fef81 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 Feb 2017 01:27:45 +0300 Subject: api: Rename dict_set_value to dict_set_var MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reasonings: 1. It is not used for anything, but scope dictionaries currenly. So there is no need to generalize and split it into dict_set_var (which will contain some scope-dictionary-specific checks) and dict_set_value (which will work for any dictionary). 2. Check for key size is no longer valid for non-scope dictionaries: you *can* use empty keys there. In scope dictionaries also, but you actually are not supposed to store there anything, but variables. Note that actually one may still do let b:[''] = 1 and “bypass” check for variable name. It won’t change what `echo b:` will show, but it may affect code which iterates over scope dictionary keys and sets them to something (if there is such code). --- src/nvim/api/buffer.c | 8 ++++---- src/nvim/api/private/helpers.c | 10 +++++----- src/nvim/api/tabpage.c | 8 ++++---- src/nvim/api/vim.c | 8 ++++---- src/nvim/api/window.c | 8 ++++---- 5 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 70246930bf..7f6e1093ed 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -456,7 +456,7 @@ void nvim_buf_set_var(Buffer buffer, String name, Object value, Error *err) return; } - dict_set_value(buf->b_vars, name, value, false, false, err); + dict_set_var(buf->b_vars, name, value, false, false, err); } /// Removes a buffer-scoped (b:) variable @@ -472,7 +472,7 @@ void nvim_buf_del_var(Buffer buffer, String name, Error *err) return; } - dict_set_value(buf->b_vars, name, NIL, true, false, err); + dict_set_var(buf->b_vars, name, NIL, true, false, err); } /// Sets a buffer-scoped (b:) variable @@ -495,7 +495,7 @@ 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, false, true, err); + return dict_set_var(buf->b_vars, name, value, false, true, err); } /// Removes a buffer-scoped (b:) variable @@ -514,7 +514,7 @@ Object buffer_del_var(Buffer buffer, String name, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(buf->b_vars, name, NIL, true, true, err); + return dict_set_var(buf->b_vars, name, NIL, true, true, err); } diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index ba4d005e9a..23f5b10e18 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -98,7 +98,7 @@ Object dict_get_value(dict_T *dict, String key, Error *err) return vim_to_object(&di->di_tv); } -/// Set a value in a dict. Objects are recursively expanded into their +/// Set a value in a scope dict. Objects are recursively expanded into their /// vimscript equivalents. /// /// @param dict The vimscript dict @@ -109,8 +109,8 @@ Object dict_get_value(dict_T *dict, String key, Error *err) /// @param retval If true the old value will be converted and returned. /// @param[out] err Details of an error that may have occurred /// @return The old value if `retval` is true and the key was present, else NIL -Object dict_set_value(dict_T *dict, String key, Object value, bool del, - bool retval, Error *err) +Object dict_set_var(dict_T *dict, String key, Object value, bool del, + bool retval, Error *err) { Object rv = OBJECT_INIT; @@ -120,7 +120,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, bool del, } if (key.size == 0) { - api_set_error(err, Validation, _("Empty dictionary keys aren't allowed")); + api_set_error(err, Validation, _("Empty variable names aren't allowed")); return rv; } @@ -129,7 +129,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, bool del, return rv; } - dictitem_T *di = dict_find(dict, (uint8_t *)key.data, (int)key.size); + dictitem_T *di = dict_find(dict, (char_u *)key.data, (int)key.size); if (del) { // Delete the key diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c index 9e61ec1871..d24250d032 100644 --- a/src/nvim/api/tabpage.c +++ b/src/nvim/api/tabpage.c @@ -71,7 +71,7 @@ void nvim_tabpage_set_var(Tabpage tabpage, return; } - dict_set_value(tab->tp_vars, name, value, false, false, err); + dict_set_var(tab->tp_vars, name, value, false, false, err); } /// Removes a tab-scoped (t:) variable @@ -87,7 +87,7 @@ void nvim_tabpage_del_var(Tabpage tabpage, String name, Error *err) return; } - dict_set_value(tab->tp_vars, name, NIL, true, false, err); + dict_set_var(tab->tp_vars, name, NIL, true, false, err); } /// Sets a tab-scoped (t:) variable @@ -110,7 +110,7 @@ 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, false, true, err); + return dict_set_var(tab->tp_vars, name, value, false, true, err); } /// Removes a tab-scoped (t:) variable @@ -129,7 +129,7 @@ Object tabpage_del_var(Tabpage tabpage, String name, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(tab->tp_vars, name, NIL, true, true, err); + return dict_set_var(tab->tp_vars, name, NIL, true, true, err); } /// Gets the current window in a tabpage diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index fd0f98c017..bf11795d9e 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -368,7 +368,7 @@ Object nvim_get_var(String name, Error *err) /// @param[out] err Error details, if any void nvim_set_var(String name, Object value, Error *err) { - dict_set_value(&globvardict, name, value, false, false, err); + dict_set_var(&globvardict, name, value, false, false, err); } /// Removes a global (g:) variable @@ -377,7 +377,7 @@ void nvim_set_var(String name, Object value, Error *err) /// @param[out] err Error details, if any void nvim_del_var(String name, Error *err) { - dict_set_value(&globvardict, name, NIL, true, false, err); + dict_set_var(&globvardict, name, NIL, true, false, err); } /// Sets a global variable @@ -393,7 +393,7 @@ void nvim_del_var(String name, Error *err) /// 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, true, err); + return dict_set_var(&globvardict, name, value, false, true, err); } /// Removes a global variable @@ -405,7 +405,7 @@ Object vim_set_var(String name, Object value, Error *err) /// @return Old value Object vim_del_var(String name, Error *err) { - return dict_set_value(&globvardict, name, NIL, true, true, err); + return dict_set_var(&globvardict, name, NIL, true, true, err); } /// Gets a v: variable diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 1f555a6a05..13294e6bf2 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -210,7 +210,7 @@ void nvim_win_set_var(Window window, String name, Object value, Error *err) return; } - dict_set_value(win->w_vars, name, value, false, false, err); + dict_set_var(win->w_vars, name, value, false, false, err); } /// Removes a window-scoped (w:) variable @@ -226,7 +226,7 @@ void nvim_win_del_var(Window window, String name, Error *err) return; } - dict_set_value(win->w_vars, name, NIL, true, false, err); + dict_set_var(win->w_vars, name, NIL, true, false, err); } /// Sets a window-scoped (w:) variable @@ -249,7 +249,7 @@ 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, false, true, err); + return dict_set_var(win->w_vars, name, value, false, true, err); } /// Removes a window-scoped (w:) variable @@ -268,7 +268,7 @@ Object window_del_var(Window window, String name, Error *err) return (Object) OBJECT_INIT; } - return dict_set_value(win->w_vars, name, NIL, true, true, err); + return dict_set_var(win->w_vars, name, NIL, true, true, err); } /// Gets a window option value -- cgit From 858ac9d8e5d971db3d6f770d386fc964187a78b4 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 Feb 2017 01:34:25 +0300 Subject: api: Make sure dict_set_var doesn’t edit read-only values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #6147 --- src/nvim/api/private/helpers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/nvim/api') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 23f5b10e18..7efa086af2 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -131,6 +131,19 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, dictitem_T *di = dict_find(dict, (char_u *)key.data, (int)key.size); + if (di != NULL) { + if (di->di_flags & DI_FLAGS_RO) { + api_set_error(err, Exception, _("Key is read-only: %s"), key.data); + return rv; + } else if (di->di_flags & DI_FLAGS_FIX) { + api_set_error(err, Exception, _("Key is fixed: %s"), key.data); + return rv; + } else if (di->di_flags & DI_FLAGS_LOCK) { + api_set_error(err, Exception, _("Key is locked: %s"), key.data); + return rv; + } + } + if (del) { // Delete the key if (di == NULL) { -- cgit