Patch 9.0.0876
Problem:    Code is indented more than needed.
Solution:   Split ExpandEscape() in two. (Yegappan Lakshmanan, closes #11539)
Files:      src/cmdexpand.c


*** ../vim-9.0.0875/src/cmdexpand.c     2022-11-12 17:44:08.264849881 +0000
--- src/cmdexpand.c     2022-11-13 22:34:54.145775708 +0000
***************
*** 92,185 ****
   * Escape special characters in the cmdline completion matches.
   */
      static void
! ExpandEscape(
      expand_T  *xp,
      char_u    *str,
      int               numfiles,
!     char_u    **files,
!     int               options)
  {
-     int               i;
      char_u    *p;
      int               vse_what = xp->xp_context == EXPAND_BUFFERS
                                                       ? VSE_BUFFER : VSE_NONE;
  
!     // May change home directory back to "~"
!     if (options & WILD_HOME_REPLACE)
!       tilde_replace(str, numfiles, files);
! 
!     if (options & WILD_ESCAPE)
!     {
!       if (xp->xp_context == EXPAND_FILES
!               || xp->xp_context == EXPAND_FILES_IN_PATH
!               || xp->xp_context == EXPAND_SHELLCMD
!               || xp->xp_context == EXPAND_BUFFERS
!               || xp->xp_context == EXPAND_DIRECTORIES)
        {
!           // Insert a backslash into a file name before a space, \, %, #
!           // and wildmatch characters, except '~'.
!           for (i = 0; i < numfiles; ++i)
            {
!               // for ":set path=" we need to escape spaces twice
!               if (xp->xp_backslash == XP_BS_THREE)
                {
                    p = vim_strsave_escaped(files[i], (char_u *)" ");
                    if (p != NULL)
                    {
                        vim_free(files[i]);
                        files[i] = p;
- #if defined(BACKSLASH_IN_FILENAME)
-                       p = vim_strsave_escaped(files[i], (char_u *)" ");
-                       if (p != NULL)
-                       {
-                           vim_free(files[i]);
-                           files[i] = p;
-                       }
- #endif
                    }
                }
  #ifdef BACKSLASH_IN_FILENAME
!               p = vim_strsave_fnameescape(files[i], vse_what);
  #else
!               p = vim_strsave_fnameescape(files[i],
!                                         xp->xp_shell ? VSE_SHELL : vse_what);
  #endif
!               if (p != NULL)
!               {
!                   vim_free(files[i]);
!                   files[i] = p;
!               }
! 
!               // If 'str' starts with "\~", replace "~" at start of
!               // files[i] with "\~".
!               if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
!                   escape_fname(&files[i]);
            }
-           xp->xp_backslash = XP_BS_NONE;
  
!           // If the first file starts with a '+' escape it.  Otherwise it
!           // could be seen as "+cmd".
!           if (*files[0] == '+')
!               escape_fname(&files[0]);
        }
!       else if (xp->xp_context == EXPAND_TAGS)
        {
!           // Insert a backslash before characters in a tag name that
!           // would terminate the ":tag" command.
!           for (i = 0; i < numfiles; ++i)
            {
!               p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
!               if (p != NULL)
!               {
!                   vim_free(files[i]);
!                   files[i] = p;
!               }
            }
        }
      }
  }
  
  /*
   * Return FAIL if this is not an appropriate context in which to do
   * completion of anything, return OK if it is (even if there are no matches).
   * For the caller, this means that the character is just passed through like a
--- 92,195 ----
   * Escape special characters in the cmdline completion matches.
   */
      static void
! wildescape(
      expand_T  *xp,
      char_u    *str,
      int               numfiles,
!     char_u    **files)
  {
      char_u    *p;
      int               vse_what = xp->xp_context == EXPAND_BUFFERS
                                                       ? VSE_BUFFER : VSE_NONE;
  
!     if (xp->xp_context == EXPAND_FILES
!           || xp->xp_context == EXPAND_FILES_IN_PATH
!           || xp->xp_context == EXPAND_SHELLCMD
!           || xp->xp_context == EXPAND_BUFFERS
!           || xp->xp_context == EXPAND_DIRECTORIES)
!     {
!       // Insert a backslash into a file name before a space, \, %, #
!       // and wildmatch characters, except '~'.
!       for (int i = 0; i < numfiles; ++i)
        {
!           // for ":set path=" we need to escape spaces twice
!           if (xp->xp_backslash == XP_BS_THREE)
            {
!               p = vim_strsave_escaped(files[i], (char_u *)" ");
!               if (p != NULL)
                {
+                   vim_free(files[i]);
+                   files[i] = p;
+ #if defined(BACKSLASH_IN_FILENAME)
                    p = vim_strsave_escaped(files[i], (char_u *)" ");
                    if (p != NULL)
                    {
                        vim_free(files[i]);
                        files[i] = p;
                    }
+ #endif
                }
+           }
  #ifdef BACKSLASH_IN_FILENAME
!           p = vim_strsave_fnameescape(files[i], vse_what);
  #else
!           p = vim_strsave_fnameescape(files[i],
!                   xp->xp_shell ? VSE_SHELL : vse_what);
  #endif
!           if (p != NULL)
!           {
!               vim_free(files[i]);
!               files[i] = p;
            }
  
!           // If 'str' starts with "\~", replace "~" at start of
!           // files[i] with "\~".
!           if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
!               escape_fname(&files[i]);
        }
!       xp->xp_backslash = XP_BS_NONE;
! 
!       // If the first file starts with a '+' escape it.  Otherwise it
!       // could be seen as "+cmd".
!       if (*files[0] == '+')
!           escape_fname(&files[0]);
!     }
!     else if (xp->xp_context == EXPAND_TAGS)
!     {
!       // Insert a backslash before characters in a tag name that
!       // would terminate the ":tag" command.
!       for (int i = 0; i < numfiles; ++i)
        {
!           p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
!           if (p != NULL)
            {
!               vim_free(files[i]);
!               files[i] = p;
            }
        }
      }
  }
  
  /*
+  * Escape special characters in the cmdline completion matches.
+  */
+     static void
+ ExpandEscape(
+     expand_T  *xp,
+     char_u    *str,
+     int               numfiles,
+     char_u    **files,
+     int               options)
+ {
+     // May change home directory back to "~"
+     if (options & WILD_HOME_REPLACE)
+       tilde_replace(str, numfiles, files);
+ 
+     if (options & WILD_ESCAPE)
+       wildescape(xp, str, numfiles, files);
+ }
+ 
+ /*
   * Return FAIL if this is not an appropriate context in which to do
   * completion of anything, return OK if it is (even if there are no matches).
   * For the caller, this means that the character is just passed through like a
***************
*** 3238,3244 ****
        garray_T        *gap)
  {
      int               ret;
-     int               i;
      hash_T    hash;
      hashitem_T        *hi;
  
--- 3248,3253 ----
***************
*** 3249,3283 ****
  
      // Expand matches in one directory of $PATH.
      ret = expand_wildcards(1, &buf, numMatches, matches, flags);
!     if (ret == OK)
      {
!       if (ga_grow(gap, *numMatches) == FAIL)
!           FreeWild(*numMatches, *matches);
!       else
        {
!           for (i = 0; i < *numMatches; ++i)
            {
!               char_u *name = (*matches)[i];
! 
!               if (STRLEN(name) > l)
!               {
!                   // Check if this name was already found.
!                   hash = hash_hash(name + l);
!                   hi = hash_lookup(ht, name + l, hash);
!                   if (HASHITEM_EMPTY(hi))
!                   {
!                       // Remove the path that was prepended.
!                       STRMOVE(name, name + l);
!                       ((char_u **)gap->ga_data)[gap->ga_len++] = name;
!                       hash_add_item(ht, hi, name, hash);
!                       name = NULL;
!                   }
!               }
!               vim_free(name);
            }
-           vim_free(*matches);
        }
      }
  }
  
  /*
--- 3258,3293 ----
  
      // Expand matches in one directory of $PATH.
      ret = expand_wildcards(1, &buf, numMatches, matches, flags);
!     if (ret != OK)
!       return;
! 
!     if (ga_grow(gap, *numMatches) == FAIL)
      {
!       FreeWild(*numMatches, *matches);
!       return;
!     }
! 
!     for (int i = 0; i < *numMatches; ++i)
!     {
!       char_u *name = (*matches)[i];
! 
!       if (STRLEN(name) > l)
        {
!           // Check if this name was already found.
!           hash = hash_hash(name + l);
!           hi = hash_lookup(ht, name + l, hash);
!           if (HASHITEM_EMPTY(hi))
            {
!               // Remove the path that was prepended.
!               STRMOVE(name, name + l);
!               ((char_u **)gap->ga_data)[gap->ga_len++] = name;
!               hash_add_item(ht, hi, name, hash);
!               name = NULL;
            }
        }
+       vim_free(name);
      }
+     vim_free(*matches);
  }
  
  /*
*** ../vim-9.0.0875/src/version.c       2022-11-13 22:13:29.852975599 +0000
--- src/version.c       2022-11-13 22:32:00.993677553 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     876,
  /**/

-- 
Over the years, I've developed my sense of deja vu so acutely that now
I can remember things that *have* happened before ...

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20221113223839.858A91C0473%40moolenaar.net.

Raspunde prin e-mail lui