Skip to content

Commit d816cd9

Browse files
committed
patch 8.2.0208: fnamemodify() does not apply ":~" when followed by ":."
Problem: Fnamemodify() does not apply ":~" when followed by ":.". Solution: Don't let a failing ":." cause the ":~" to be skipped. (Yasuhiro Matsumoto, closes #5577)
1 parent bfe1204 commit d816cd9

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

runtime/doc/cmdline.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,7 @@ These modifiers can be given, in this order:
933933
directory.
934934
:. Reduce file name to be relative to current directory, if
935935
possible. File name is unmodified if it is not below the
936-
current directory, but on MS-Windows the drive is removed if
937-
it is the current drive.
936+
current directory.
938937
For maximum shortness, use ":~:.".
939938
:h Head of the file name (the last component and any separators
940939
removed). Cannot be used with :e, :r or :t.

src/filepath.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ modify_fname(
301301
char_u dirname[MAXPATHL];
302302
int c;
303303
int has_fullname = 0;
304+
int has_homerelative = 0;
304305
#ifdef MSWIN
305306
char_u *fname_start = *fnamep;
306307
int has_shortname = 0;
@@ -412,7 +413,7 @@ modify_fname(
412413
}
413414
pbuf = NULL;
414415
// Need full path first (use expand_env() to remove a "~/")
415-
if (!has_fullname)
416+
if (!has_fullname && !has_homerelative)
416417
{
417418
if (c == '.' && **fnamep == '~')
418419
p = pbuf = expand_env_save(*fnamep);
@@ -428,11 +429,28 @@ modify_fname(
428429
{
429430
if (c == '.')
430431
{
432+
size_t namelen;
433+
431434
mch_dirname(dirname, MAXPATHL);
432-
s = shorten_fname(p, dirname);
433-
if (s != NULL)
435+
if (has_homerelative)
436+
{
437+
s = vim_strsave(dirname);
438+
if (s != NULL)
439+
{
440+
home_replace(NULL, s, dirname, MAXPATHL, TRUE);
441+
vim_free(s);
442+
}
443+
}
444+
namelen = STRLEN(dirname);
445+
446+
// Do not call shorten_fname() here since it removes the prefix
447+
// even though the path does not have a prefix.
448+
if (fnamencmp(p, dirname, namelen) == 0)
434449
{
435-
*fnamep = s;
450+
p += namelen;
451+
while (*p && vim_ispathsep(*p))
452+
++p;
453+
*fnamep = p;
436454
if (pbuf != NULL)
437455
{
438456
vim_free(*bufp); // free any allocated file name
@@ -453,6 +471,7 @@ modify_fname(
453471
*fnamep = s;
454472
vim_free(*bufp);
455473
*bufp = s;
474+
has_homerelative = TRUE;
456475
}
457476
}
458477
}
@@ -701,6 +720,7 @@ f_chdir(typval_T *argvars, typval_T *rettv)
701720
rettv->vval.v_string = NULL;
702721

703722
if (argvars[0].v_type != VAR_STRING)
723+
// Returning an empty string means it failed.
704724
return;
705725

706726
// Return the current directory

src/testdir/test_fnamemodify.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
func Test_fnamemodify()
44
let save_home = $HOME
55
let save_shell = &shell
6+
let save_shellslash = &shellslash
67
let $HOME = fnamemodify('.', ':p:h:h')
78
set shell=sh
9+
set shellslash
810

911
call assert_equal('/', fnamemodify('.', ':p')[-1:])
1012
call assert_equal('r', fnamemodify('.', ':p:h')[-1:])
@@ -28,6 +30,15 @@ func Test_fnamemodify()
2830
call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e:e'))
2931
call assert_equal('tar', fnamemodify('abc.fb2.tar.gz', ':e:e:r'))
3032

33+
let cwd = getcwd()
34+
call mkdir($HOME . '/XXXXXXXX/a', 'p')
35+
call mkdir($HOME . '/XXXXXXXX/b', 'p')
36+
call chdir($HOME . '/XXXXXXXX/a/')
37+
call assert_equal('foo', fnamemodify($HOME . '/XXXXXXXX/a/foo', ':p:~:.'))
38+
call assert_equal('~/XXXXXXXX/b/foo', fnamemodify($HOME . '/XXXXXXXX/b/foo', ':p:~:.'))
39+
call chdir(cwd)
40+
call delete($HOME . '/XXXXXXXX', 'rf')
41+
3142
call assert_equal('''abc def''', fnamemodify('abc def', ':S'))
3243
call assert_equal('''abc" "def''', fnamemodify('abc" "def', ':S'))
3344
call assert_equal('''abc"%"def''', fnamemodify('abc"%"def', ':S'))
@@ -44,6 +55,7 @@ func Test_fnamemodify()
4455

4556
let $HOME = save_home
4657
let &shell = save_shell
58+
let &shellslash = save_shellslash
4759
endfunc
4860

4961
func Test_fnamemodify_er()

src/version.c

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

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
208,
745747
/**/
746748
207,
747749
/**/

0 commit comments

Comments
 (0)