Skip to content

Commit 00136dc

Browse files
committed
patch 8.1.0211: expanding a file name "~" results in $HOME
Problem: Expanding a file name "~" results in $HOME. (Aidan Shafran) Solution: Change "~" to "./~" before expanding. (closes #3072)
1 parent 6f8d2ac commit 00136dc

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

src/eval.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9690,11 +9690,12 @@ shortpath_for_partial(
96909690
*/
96919691
int
96929692
modify_fname(
9693-
char_u *src, /* string with modifiers */
9694-
int *usedlen, /* characters after src that are used */
9695-
char_u **fnamep, /* file name so far */
9696-
char_u **bufp, /* buffer for allocated file name or NULL */
9697-
int *fnamelen) /* length of fnamep */
9693+
char_u *src, // string with modifiers
9694+
int tilde_file, // "~" is a file name, not $HOME
9695+
int *usedlen, // characters after src that are used
9696+
char_u **fnamep, // file name so far
9697+
char_u **bufp, // buffer for allocated file name or NULL
9698+
int *fnamelen) // length of fnamep
96989699
{
96999700
int valid = 0;
97009701
char_u *tail;
@@ -9724,8 +9725,8 @@ modify_fname(
97249725
|| (*fnamep)[1] == '\\'
97259726
# endif
97269727
|| (*fnamep)[1] == NUL)
9727-
97289728
#endif
9729+
&& !(tilde_file && (*fnamep)[1] == NUL)
97299730
)
97309731
{
97319732
*fnamep = expand_env_save(*fnamep);

src/evalfunc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3801,7 +3801,7 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
38013801
else
38023802
{
38033803
len = (int)STRLEN(fname);
3804-
(void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3804+
(void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
38053805
}
38063806

38073807
rettv->v_type = VAR_STRING;

src/ex_docmd.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10654,6 +10654,7 @@ eval_vars(
1065410654
int resultlen;
1065510655
buf_T *buf;
1065610656
int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10657+
int tilde_file = FALSE;
1065710658
int spec_idx;
1065810659
#ifdef FEAT_MODIFY_FNAME
1065910660
int skip_mod = FALSE;
@@ -10720,7 +10721,10 @@ eval_vars(
1072010721
valid = 0; /* Must have ":p:h" to be valid */
1072110722
}
1072210723
else
10724+
{
1072310725
result = curbuf->b_fname;
10726+
tilde_file = STRCMP(result, "~") == 0;
10727+
}
1072410728
break;
1072510729

1072610730
case SPEC_HASH: /* '#' or "#99": alternate file */
@@ -10784,7 +10788,10 @@ eval_vars(
1078410788
valid = 0; /* Must have ":p:h" to be valid */
1078510789
}
1078610790
else
10791+
{
1078710792
result = buf->b_fname;
10793+
tilde_file = STRCMP(result, "~") == 0;
10794+
}
1078810795
}
1078910796
break;
1079010797

@@ -10877,7 +10884,7 @@ eval_vars(
1087710884
#ifdef FEAT_MODIFY_FNAME
1087810885
else if (!skip_mod)
1087910886
{
10880-
valid |= modify_fname(src, usedlen, &result, &resultbuf,
10887+
valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
1088110888
&resultlen);
1088210889
if (result == NULL)
1088310890
{

src/if_cscope.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ cs_add_common(
519519
#ifdef FEAT_MODIFY_FNAME
520520
len = (int)STRLEN(fname);
521521
fbuf = (char_u *)fname;
522-
(void)modify_fname((char_u *)":p", &usedlen,
522+
(void)modify_fname((char_u *)":p", FALSE, &usedlen,
523523
(char_u **)&fname, &fbuf, &len);
524524
if (fname == NULL)
525525
goto add_err;

src/misc1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4908,7 +4908,7 @@ home_replace(
49084908
char_u *fbuf = NULL;
49094909

49104910
flen = (int)STRLEN(homedir_env);
4911-
(void)modify_fname((char_u *)":p", &usedlen,
4911+
(void)modify_fname((char_u *)":p", FALSE, &usedlen,
49124912
&homedir_env, &fbuf, &flen);
49134913
flen = (int)STRLEN(homedir_env);
49144914
if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))

src/proto/eval.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typ
136136
int typval_compare(typval_T *typ1, typval_T *typ2, exptype_T type, int type_is, int ic);
137137
char_u *typval_tostring(typval_T *arg);
138138
int var_exists(char_u *var);
139-
int modify_fname(char_u *src, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
139+
int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
140140
char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, typval_T *expr, char_u *flags);
141141
void filter_map(typval_T *argvars, typval_T *rettv, int map);
142142
/* vim: set ft=c : */

src/testdir/test_expand.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ func Test_with_tilde()
3939
call delete('Xdir ~ dir', 'd')
4040
call assert_false(isdirectory('Xdir ~ dir'))
4141
endfunc
42+
43+
func Test_expand_tilde_filename()
44+
split ~
45+
call assert_equal('~', expand('%'))
46+
call assert_notequal(expand('%:p'), expand('~/'))
47+
call assert_match('\~', expand('%:p'))
48+
bwipe!
49+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,8 @@ static char *(features[]) =
793793

794794
static int included_patches[] =
795795
{ /* Add new patch number below this line */
796+
/**/
797+
211,
796798
/**/
797799
210,
798800
/**/

0 commit comments

Comments
 (0)