]> granicus.if.org Git - vim/commitdiff
patch 8.1.0211: expanding a file name "~" results in $HOME v8.1.0211
authorBram Moolenaar <Bram@vim.org>
Wed, 25 Jul 2018 19:19:13 +0000 (21:19 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 25 Jul 2018 19:19:13 +0000 (21:19 +0200)
Problem:    Expanding a file name "~" results in $HOME. (Aidan Shafran)
Solution:   Change "~" to "./~" before expanding. (closes #3072)

src/eval.c
src/evalfunc.c
src/ex_docmd.c
src/if_cscope.c
src/misc1.c
src/proto/eval.pro
src/testdir/test_expand.vim
src/version.c

index ef25a3fc9d62233e085a37fb57d212927622b4d3..6034cd24f74d8b609d4b33ba18b3f4184d43797f 100644 (file)
@@ -9690,11 +9690,12 @@ shortpath_for_partial(
  */
     int
 modify_fname(
-    char_u     *src,           /* string with modifiers */
-    int                *usedlen,       /* characters after src that are used */
-    char_u     **fnamep,       /* file name so far */
-    char_u     **bufp,         /* buffer for allocated file name or NULL */
-    int                *fnamelen)      /* length of fnamep */
+    char_u     *src,           // string with modifiers
+    int                tilde_file,     // "~" is a file name, not $HOME
+    int                *usedlen,       // characters after src that are used
+    char_u     **fnamep,       // file name so far
+    char_u     **bufp,         // buffer for allocated file name or NULL
+    int                *fnamelen)      // length of fnamep
 {
     int                valid = 0;
     char_u     *tail;
@@ -9724,8 +9725,8 @@ repeat:
                    || (*fnamep)[1] == '\\'
 # endif
                    || (*fnamep)[1] == NUL)
-
 #endif
+               && !(tilde_file && (*fnamep)[1] == NUL)
           )
        {
            *fnamep = expand_env_save(*fnamep);
index 97242f682c79477ac89513cf24fef774819739c9..a9f6c5b8a6ee1cbe09235682d041dd80f4224a85 100644 (file)
@@ -3801,7 +3801,7 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
     else
     {
        len = (int)STRLEN(fname);
-       (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
+       (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
     }
 
     rettv->v_type = VAR_STRING;
index 4b3fdac37542c3aa2ed0e3348de6035420193ca4..0094cdba7d29f19a836f766d3259528849e6407a 100644 (file)
@@ -10654,6 +10654,7 @@ eval_vars(
     int                resultlen;
     buf_T      *buf;
     int                valid = VALID_HEAD + VALID_PATH;    /* assume valid result */
+    int                tilde_file = FALSE;
     int                spec_idx;
 #ifdef FEAT_MODIFY_FNAME
     int                skip_mod = FALSE;
@@ -10720,7 +10721,10 @@ eval_vars(
                    valid = 0;      /* Must have ":p:h" to be valid */
                }
                else
+               {
                    result = curbuf->b_fname;
+                   tilde_file = STRCMP(result, "~") == 0;
+               }
                break;
 
        case SPEC_HASH:         /* '#' or "#99": alternate file */
@@ -10784,7 +10788,10 @@ eval_vars(
                        valid = 0;          /* Must have ":p:h" to be valid */
                    }
                    else
+                   {
                        result = buf->b_fname;
+                       tilde_file = STRCMP(result, "~") == 0;
+                   }
                }
                break;
 
@@ -10877,7 +10884,7 @@ eval_vars(
 #ifdef FEAT_MODIFY_FNAME
        else if (!skip_mod)
        {
-           valid |= modify_fname(src, usedlen, &result, &resultbuf,
+           valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
                                                                  &resultlen);
            if (result == NULL)
            {
index 986663d9516c9233a42ebb5cd9d78cfcc9f04a1a..d621a146b310ed7a68ece70894bab396b39efd51 100644 (file)
@@ -519,7 +519,7 @@ cs_add_common(
 #ifdef FEAT_MODIFY_FNAME
     len = (int)STRLEN(fname);
     fbuf = (char_u *)fname;
-    (void)modify_fname((char_u *)":p", &usedlen,
+    (void)modify_fname((char_u *)":p", FALSE, &usedlen,
                                              (char_u **)&fname, &fbuf, &len);
     if (fname == NULL)
        goto add_err;
index f0e629b5bd085996072ec21c2820c309daa94ec2..ab5f6f73193dafdf949f1cabad74b55f91cf6afa 100644 (file)
@@ -4908,7 +4908,7 @@ home_replace(
        char_u  *fbuf = NULL;
 
        flen = (int)STRLEN(homedir_env);
-       (void)modify_fname((char_u *)":p", &usedlen,
+       (void)modify_fname((char_u *)":p", FALSE, &usedlen,
                                                  &homedir_env, &fbuf, &flen);
        flen = (int)STRLEN(homedir_env);
        if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
index 0906a68d3f77b3c02c5787a3a0952e84ebdad53b..98f6656717a9ccfc9740c0e4014122b57183a88a 100644 (file)
@@ -136,7 +136,7 @@ void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typ
 int typval_compare(typval_T *typ1, typval_T *typ2, exptype_T type, int type_is, int ic);
 char_u *typval_tostring(typval_T *arg);
 int var_exists(char_u *var);
-int modify_fname(char_u *src, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
+int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
 char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, typval_T *expr, char_u *flags);
 void filter_map(typval_T *argvars, typval_T *rettv, int map);
 /* vim: set ft=c : */
index c099edae1cc4f58990143c3984f96ab1d8654914..b4f1363d12cfce4469d55c93f7f67f0705aff9de 100644 (file)
@@ -39,3 +39,11 @@ func Test_with_tilde()
   call delete('Xdir ~ dir', 'd')
   call assert_false(isdirectory('Xdir ~ dir'))
 endfunc
+
+func Test_expand_tilde_filename()
+  split ~
+  call assert_equal('~', expand('%')) 
+  call assert_notequal(expand('%:p'), expand('~/'))
+  call assert_match('\~', expand('%:p')) 
+  bwipe!
+endfunc
index 456dc12ee6018d9461bf560916c6a0b7ff3fbaf8..24753d1e5591ed0f77cc74af58242a868a2210a5 100644 (file)
@@ -793,6 +793,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    211,
 /**/
     210,
 /**/