diff options
author | zeertzjq <zeertzjq@outlook.com> | 2025-03-04 07:37:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-03 23:37:14 +0000 |
commit | 47cfe901d739149f88b6f917fc1f310727700b40 (patch) | |
tree | e667b0c753bc286b746ada0d8a5cddde459d106c | |
parent | 8ce504820af04194a41acbe1f4c61cf12bd5feb5 (diff) | |
download | rneovim-47cfe901d739149f88b6f917fc1f310727700b40.tar.gz rneovim-47cfe901d739149f88b6f917fc1f310727700b40.tar.bz2 rneovim-47cfe901d739149f88b6f917fc1f310727700b40.zip |
vim-patch:9.1.1169: using global variable for get_insert()/get_lambda_name() (#32713)
Problem: using global variable for get_insert()/get_lambda_name()
(after v9.1.1151)
Solution: let the functions return a string_T object instead
(Yee Cheng Chin)
In vim/vim#16720, `get_insert()` was modified to store a string length in a
global variable to be queried immediately by another `get_insert_len()`
function, which is somewhat fragile. Instead, just have the function
itself return a `string_T` object instead. Also do the same for
`get_lambda_name()` which has similar issues.
closes: vim/vim#16775
https://github.com/vim/vim/commit/0b5fe420715786249cd121d845dbd6a81f962d1b
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
-rw-r--r-- | src/nvim/edit.c | 40 | ||||
-rw-r--r-- | src/nvim/eval/userfunc.c | 31 | ||||
-rw-r--r-- | src/nvim/getchar.c | 15 | ||||
-rw-r--r-- | src/nvim/getchar.h | 1 |
4 files changed, 29 insertions, 58 deletions
diff --git a/src/nvim/edit.c b/src/nvim/edit.c index f5ba9c1c05..c3f204045a 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -9,6 +9,7 @@ #include <uv.h> #include "klib/kvec.h" +#include "nvim/api/private/defs.h" #include "nvim/ascii_defs.h" #include "nvim/autocmd.h" #include "nvim/autocmd_defs.h" @@ -99,7 +100,6 @@ typedef struct { int did_restart_edit; // remember if insert mode was restarted // after a ctrl+o bool nomove; - char *ptr; } InsertState; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -163,15 +163,8 @@ static void insert_enter(InsertState *s) if (s->cmdchar != 'r' && s->cmdchar != 'v') { pos_T save_cursor = curwin->w_cursor; - if (s->cmdchar == 'R') { - s->ptr = "r"; - } else if (s->cmdchar == 'V') { - s->ptr = "v"; - } else { - s->ptr = "i"; - } - - set_vim_var_string(VV_INSERTMODE, s->ptr, 1); + const char *const ptr = s->cmdchar == 'R' ? "r" : s->cmdchar == 'V' ? "v" : "i"; + set_vim_var_string(VV_INSERTMODE, ptr, 1); set_vim_var_string(VV_CHAR, NULL, -1); ins_apply_autocmds(EVENT_INSERTENTER); @@ -288,14 +281,15 @@ static void insert_enter(InsertState *s) // column. Eg after "^O$" or "^O80|". validate_virtcol(curwin); update_curswant(); + const char *ptr; if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) || curwin->w_curswant > curwin->w_virtcol) - && *(s->ptr = get_cursor_line_ptr() + curwin->w_cursor.col) != NUL) { - if (s->ptr[1] == NUL) { + && *(ptr = get_cursor_line_ptr() + curwin->w_cursor.col) != NUL) { + if (ptr[1] == NUL) { curwin->w_cursor.col++; } else { - s->i = utfc_ptr2len(s->ptr); - if (s->ptr[s->i] == NUL) { + s->i = utfc_ptr2len(ptr); + if (ptr[s->i] == NUL) { curwin->w_cursor.col += s->i; } } @@ -335,12 +329,10 @@ static void insert_enter(InsertState *s) // Get the current length of the redo buffer, those characters have to be // skipped if we want to get to the inserted characters. - s->ptr = get_inserted(); - if (s->ptr == NULL) { - new_insert_skip = 0; - } else { - new_insert_skip = (int)get_inserted_len(); - xfree(s->ptr); + String inserted = get_inserted(); + new_insert_skip = (int)inserted.size; + if (inserted.data != NULL) { + xfree(inserted.data); } old_indent = 0; @@ -2337,14 +2329,14 @@ static void stop_insert(pos_T *end_insert_pos, int esc, int nomove) // Save the inserted text for later redo with ^@ and CTRL-A. // Don't do it when "restart_edit" was set and nothing was inserted, // otherwise CTRL-O w and then <Left> will clear "last_insert". - char *ptr = get_inserted(); - int added = ptr == NULL ? 0 : (int)get_inserted_len() - new_insert_skip; + String inserted = get_inserted(); + int added = inserted.data == NULL ? 0 : (int)inserted.size - new_insert_skip; if (did_restart_edit == 0 || added > 0) { xfree(last_insert); - last_insert = ptr; + last_insert = inserted.data; last_insert_skip = added < 0 ? 0 : new_insert_skip; } else { - xfree(ptr); + xfree(inserted.data); } if (!arrow_used && end_insert_pos != NULL) { diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index f386dd28b9..c5357d507c 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -8,6 +8,7 @@ #include <stdlib.h> #include <string.h> +#include "nvim/api/private/defs.h" #include "nvim/ascii_defs.h" #include "nvim/autocmd.h" #include "nvim/autocmd_defs.h" @@ -265,29 +266,16 @@ static void register_closure(ufunc_T *fp) } static char lambda_name[8 + NUMBUFLEN]; -static size_t lambda_namelen = 0; /// @return a name for a lambda. Returned in static memory. -char *get_lambda_name(void) +static String get_lambda_name(void) { static int lambda_no = 0; int n = snprintf(lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no); - if (n < 1) { - lambda_namelen = 0; - } else if (n >= (int)sizeof(lambda_name)) { - lambda_namelen = sizeof(lambda_name) - 1; - } else { - lambda_namelen = (size_t)n; - } - return lambda_name; -} - -/// Get the length of the last lambda name. -size_t get_lambda_name_len(void) -{ - return lambda_namelen; + return cbuf_as_string(lambda_name, + n < 1 ? 0 : (size_t)MIN(n, (int)sizeof(lambda_name) - 1)); } /// Allocate a "ufunc_T" for a function called "name". @@ -371,10 +359,8 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg) int flags = 0; garray_T newlines; - char *name = get_lambda_name(); - size_t namelen = get_lambda_name_len(); - - fp = alloc_ufunc(name, namelen); + String name = get_lambda_name(); + fp = alloc_ufunc(name.data, name.size); pt = xcalloc(1, sizeof(partial_T)); ga_init(&newlines, (int)sizeof(char *), 1); @@ -4142,9 +4128,8 @@ bool set_ref_in_func(char *name, ufunc_T *fp_in, int copyID) /// Registers a luaref as a lambda. char *register_luafunc(LuaRef ref) { - char *name = get_lambda_name(); - size_t namelen = get_lambda_name_len(); - ufunc_T *fp = alloc_ufunc(name, namelen); + String name = get_lambda_name(); + ufunc_T *fp = alloc_ufunc(name.data, name.size); fp->uf_refcount = 1; fp->uf_varargs = true; diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index ac817ab9e6..899293e9f6 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -155,9 +155,6 @@ static uint8_t noremapbuf_init[TYPELEN_INIT]; ///< initial typebuf.tb_noremap static size_t last_recorded_len = 0; ///< number of last recorded chars -static size_t last_get_inserted_len = 0; ///< length of the string returned from the - ///< last call to get_inserted() - enum { KEYLEN_PART_KEY = -1, ///< keylen value for incomplete key-code KEYLEN_PART_MAP = -2, ///< keylen value for incomplete mapping @@ -254,15 +251,11 @@ char *get_recorded(void) /// Return the contents of the redo buffer as a single string. /// K_SPECIAL in the returned string is escaped. -char *get_inserted(void) -{ - return get_buffcont(&redobuff, false, &last_get_inserted_len); -} - -/// Return the length of string returned from the last call of get_inserted(). -size_t get_inserted_len(void) +String get_inserted(void) { - return last_get_inserted_len; + size_t len = 0; + char *str = get_buffcont(&redobuff, false, &len); + return cbuf_as_string(str, len); } /// Add string after the current block of the given buffer diff --git a/src/nvim/getchar.h b/src/nvim/getchar.h index 766c5eb2a4..aa5110655c 100644 --- a/src/nvim/getchar.h +++ b/src/nvim/getchar.h @@ -3,6 +3,7 @@ #include <stddef.h> // IWYU pragma: keep #include <stdint.h> // IWYU pragma: keep +#include "nvim/api/private/defs.h" // IWYU pragma: keep #include "nvim/eval/typval_defs.h" // IWYU pragma: keep #include "nvim/getchar_defs.h" // IWYU pragma: keep #include "nvim/types_defs.h" // IWYU pragma: keep |