]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1169: using global variable for get_insert()/get_lambda_name() v9.1.1169
authorYee Cheng Chin <ychin.git@gmail.com>
Mon, 3 Mar 2025 19:12:05 +0000 (20:12 +0100)
committerChristian Brabandt <cb@256bit.org>
Mon, 3 Mar 2025 19:12:05 +0000 (20:12 +0100)
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 #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: #16775

Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/edit.c
src/getchar.c
src/proto/getchar.pro
src/proto/userfunc.pro
src/userfunc.c
src/version.c
src/vim9compile.c

index 808aae1416b6c245142fb134b08d2ec113460a1b..f15a19de2cb3df43b870494fbb35d0464bce09ea 100644 (file)
@@ -415,14 +415,10 @@ edit(
      * Get the current length of the redo buffer, those characters have to be
      * skipped if we want to get to the inserted characters.
      */
-    ptr = get_inserted();
-    if (ptr == NULL)
-       new_insert_skip = 0;
-    else
-    {
-       new_insert_skip = (int)get_inserted_len();
-       vim_free(ptr);
-    }
+    string_T inserted = get_inserted();
+    new_insert_skip = (int)inserted.length;
+    if (inserted.string != NULL)
+       vim_free(inserted.string);
 
     old_indent = 0;
 
@@ -2445,7 +2441,7 @@ stop_insert(
     int                nomove)                 // <c-\><c-o>, don't move cursor
 {
     int                cc;
-    char_u     *ptr;
+    string_T   inserted;
 
     stop_redo_ins();
     replace_flush();           // abandon replace stack
@@ -2455,16 +2451,16 @@ stop_insert(
      * Don't do it when "restart_edit" was set and nothing was inserted,
      * otherwise CTRL-O w and then <Left> will clear "last_insert".
      */
-    ptr = get_inserted();
-    int added = ptr == NULL ? 0 : (int)get_inserted_len() - new_insert_skip;
+    inserted = get_inserted();
+    int added = inserted.string == NULL ? 0 : (int)inserted.length - new_insert_skip;
     if (did_restart_edit == 0 || added > 0)
     {
        vim_free(last_insert);
-       last_insert = ptr;
+       last_insert = inserted.string;
        last_insert_skip = added < 0 ? 0 : new_insert_skip;
     }
     else
-       vim_free(ptr);
+       vim_free(inserted.string);
 
     if (!arrow_used && end_insert_pos != NULL)
     {
index 014e5759fe1b540662f2055e9d6b69d053eb6098..05ae373933d59eb7759bb454e1a9a27162dee3a3 100644 (file)
@@ -88,9 +88,6 @@ static char_u 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()
-
 #ifdef FEAT_EVAL
 mapblock_T     *last_used_map = NULL;
 int            last_used_sid = -1;
@@ -203,19 +200,13 @@ get_recorded(void)
  * Return the contents of the redo buffer as a single string.
  * K_SPECIAL and CSI in the returned string are escaped.
  */
-    char_u *
+    string_T
 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)
-{
-    return last_get_inserted_len;
+    size_t len = 0;
+    char_u* str = get_buffcont(&redobuff, FALSE, &len);
+    string_T ret = { str, len };
+    return ret;
 }
 
 /*
index c1eb1abadd598c1651bc549ccfde0b2ac07666a0..11a37fb1c52e8fedc6e06da897cd3985f9bc9fc6 100644 (file)
@@ -1,7 +1,6 @@
 /* getchar.c */
 char_u *get_recorded(void);
-char_u *get_inserted(void);
-size_t get_inserted_len(void);
+string_T get_inserted(void);
 int stuff_empty(void);
 int readbuf1_empty(void);
 void typeahead_noflush(int c);
index a0f04b206b2fdd8e95a79949eecd31deedf2cc75..95d8cc4f0b637863267cd5cd4f3ebf05eb29582f 100644 (file)
@@ -2,8 +2,7 @@
 void func_init(void);
 hashtab_T *func_tbl_get(void);
 char_u *make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize);
-char_u *get_lambda_name(void);
-size_t get_lambda_name_len(void);
+string_T get_lambda_name(void);
 char_u *register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state);
 int get_lambda_tv(char_u **arg, typval_T *rettv, int types_optional, evalarg_T *evalarg);
 char_u *deref_func_name(char_u *name, int *lenp, partial_T **partialp, type_T **type, int no_autoload, int new_function, int *found_var);
index b7d2752dca27d9f2cb5dc1bcfc5f41cdbc918b86..531b67a550271200c380cb551157c7fce90b660f 100644 (file)
@@ -683,36 +683,28 @@ make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
 }
 
 static char_u  lambda_name[8 + NUMBUFLEN];
-static size_t  lambda_namelen = 0;
 
 /*
  * Get a name for a lambda.  Returned in static memory.
  */
-    char_u *
+    string_T
 get_lambda_name(void)
 {
     static int lambda_no = 0;
     int                n;
+    string_T   ret;
 
     n = vim_snprintf((char *)lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no);
     if (n < 1)
-       lambda_namelen = 0;
+       ret.length = 0;
     else
     if (n >= (int)sizeof(lambda_name))
-       lambda_namelen = sizeof(lambda_name) - 1;
+       ret.length = sizeof(lambda_name) - 1;
     else
-       lambda_namelen = (size_t)n;
-
-    return lambda_name;
-}
+       ret.length = (size_t)n;
 
-/*
- * Get the length of the last lambda name.
- */
-    size_t
-get_lambda_name_len(void)
-{
-    return lambda_namelen;
+    ret.string = lambda_name;
+    return ret;
 }
 
 /*
@@ -756,11 +748,10 @@ alloc_ufunc(char_u *name, size_t namelen)
     char_u *
 register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
 {
-    char_u     *name = get_lambda_name();
-    size_t     namelen = get_lambda_name_len();
+    string_T   name = get_lambda_name();
     ufunc_T    *fp;
 
-    fp = alloc_ufunc(name, namelen);
+    fp = alloc_ufunc(name.string, name.length);
     if (fp == NULL)
        return NULL;
 
@@ -776,7 +767,7 @@ register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
 
     hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
 
-    return name;
+    return name.string;
 }
 #endif
 
@@ -1477,8 +1468,7 @@ lambda_function_body(
     char_u     *cmdline = NULL;
     int                ret = FAIL;
     partial_T  *pt;
-    char_u     *name;
-    size_t     namelen;
+    string_T   name;
     int                lnum_save = -1;
     linenr_T   sourcing_lnum_top = SOURCING_LNUM;
     char_u     *line_arg = NULL;
@@ -1597,8 +1587,7 @@ lambda_function_body(
     }
 
     name = get_lambda_name();
-    namelen = get_lambda_name_len();
-    ufunc = alloc_ufunc(name, namelen);
+    ufunc = alloc_ufunc(name.string, name.length);
     if (ufunc == NULL)
        goto erret;
     if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
@@ -1805,10 +1794,9 @@ get_lambda_tv(
        int         flags = FC_LAMBDA;
        char_u      *p;
        char_u      *line_end;
-       char_u      *name = get_lambda_name();
-       size_t      namelen = get_lambda_name_len();
+       string_T    name = get_lambda_name();
 
-       fp = alloc_ufunc(name, namelen);
+       fp = alloc_ufunc(name.string, name.length);
        if (fp == NULL)
            goto errret;
        fp->uf_def_status = UF_NOT_COMPILED;
index 190528b9d8862226976498cccb37f8e9fc1e5323..3d5f8693810fc26b5975d6719469abe883dced61 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1169,
 /**/
     1168,
 /**/
index 67eeda857995c7df18c49a4c5ff0cd3908251b9a..9d0bf71defecb11c8dfaace3521ee9a378fe0a54 100644 (file)
@@ -1033,8 +1033,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     char_u     *name_end = to_name_end(eap->arg, TRUE);
     int                off;
     char_u     *func_name;
-    char_u     *lambda_name;
-    size_t     lambda_namelen;
+    string_T   lambda_name;
     ufunc_T    *ufunc;
     int                r = FAIL;
     compiletype_T   compile_type;
@@ -1094,9 +1093,8 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     eap->forceit = FALSE;
     // We use the special <Lamba>99 name, but it's not really a lambda.
     lambda_name = get_lambda_name();
-    lambda_namelen = get_lambda_name_len();
-    lambda_name = vim_strnsave(lambda_name, lambda_namelen);
-    if (lambda_name == NULL)
+    lambda_name.string = vim_strnsave(lambda_name.string, lambda_name.length);
+    if (lambda_name.string == NULL)
        return NULL;
 
     // This may free the current line, make a copy of the name.
@@ -1112,7 +1110,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     int save_KeyTyped = KeyTyped;
     KeyTyped = FALSE;
 
-    ufunc = define_function(eap, lambda_name, lines_to_free, 0, NULL, 0);
+    ufunc = define_function(eap, lambda_name.string, lines_to_free, 0, NULL, 0);
 
     KeyTyped = save_KeyTyped;
 
@@ -1148,9 +1146,10 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     // recursive call.
     if (is_global)
     {
-       r = generate_NEWFUNC(cctx, lambda_name, func_name);
+       r = generate_NEWFUNC(cctx, lambda_name.string, func_name);
        func_name = NULL;
-       lambda_name = NULL;
+       lambda_name.string = NULL;
+       lambda_name.length = 0;
     }
     else
     {
@@ -1197,7 +1196,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     }
 
 theend:
-    vim_free(lambda_name);
+    vim_free(lambda_name.string);
     vim_free(func_name);
     return r == FAIL ? NULL : (char_u *)"";
 }