vim-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 vim/vim#5577)
d816cd94d8
This commit is contained in:
zeertzjq 2022-02-07 06:48:10 +08:00
parent 380bc4fe22
commit d457168e3b
3 changed files with 34 additions and 9 deletions

View File

@ -908,8 +908,7 @@ These modifiers can be given, in this order:
directory.
:. Reduce file name to be relative to current directory, if
possible. File name is unmodified if it is not below the
current directory, but on MS-Windows the drive is removed if
it is the current drive.
current directory.
For maximum shortness, use ":~:.".
:h Head of the file name (the last component and any separators
removed). Cannot be used with :e, :r or :t.

View File

@ -10624,12 +10624,13 @@ int modify_fname(char_u *src, bool tilde_file, size_t *usedlen, char_u **fnamep,
char_u *s, *p, *pbuf;
char_u dirname[MAXPATHL];
int c;
int has_fullname = 0;
bool has_fullname = false;
bool has_homerelative = false;
repeat:
// ":p" - full path/file_name
if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') {
has_fullname = 1;
has_fullname = true;
valid |= VALID_PATH;
*usedlen += 2;
@ -10698,7 +10699,7 @@ repeat:
}
pbuf = NULL;
// Need full path first (use expand_env() to remove a "~/")
if (!has_fullname) {
if (!has_fullname && !has_homerelative) {
if (c == '.' && **fnamep == '~') {
p = pbuf = expand_env_save(*fnamep);
} else {
@ -10708,14 +10709,26 @@ repeat:
p = *fnamep;
}
has_fullname = 0;
has_fullname = false;
if (p != NULL) {
if (c == '.') {
os_dirname(dirname, MAXPATHL);
s = path_shorten_fname(p, dirname);
if (s != NULL) {
*fnamep = s;
if (has_homerelative) {
s = vim_strsave(dirname);
home_replace(NULL, s, dirname, MAXPATHL, true);
xfree(s);
}
size_t namelen = STRLEN(dirname);
// Do not call shorten_fname() here since it removes the prefix
// even though the path does not have a prefix.
if (fnamencmp(p, dirname, namelen) == 0) {
p += namelen;
while (*p && vim_ispathsep(*p)) {
++p;
}
*fnamep = p;
if (pbuf != NULL) {
xfree(*bufp); // free any allocated file name
*bufp = pbuf;
@ -10730,6 +10743,7 @@ repeat:
*fnamep = s;
xfree(*bufp);
*bufp = s;
has_homerelative = true;
}
}
xfree(pbuf);

View File

@ -3,8 +3,10 @@
func Test_fnamemodify()
let save_home = $HOME
let save_shell = &shell
let save_shellslash = &shellslash
let $HOME = fnamemodify('.', ':p:h:h')
set shell=sh
set shellslash
call assert_equal('/', fnamemodify('.', ':p')[-1:])
call assert_equal('r', fnamemodify('.', ':p:h')[-1:])
@ -28,6 +30,15 @@ func Test_fnamemodify()
call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e:e'))
call assert_equal('tar', fnamemodify('abc.fb2.tar.gz', ':e:e:r'))
let cwd = getcwd()
call mkdir($HOME . '/XXXXXXXX/a', 'p')
call mkdir($HOME . '/XXXXXXXX/b', 'p')
call chdir($HOME . '/XXXXXXXX/a/')
call assert_equal('foo', fnamemodify($HOME . '/XXXXXXXX/a/foo', ':p:~:.'))
call assert_equal('~/XXXXXXXX/b/foo', fnamemodify($HOME . '/XXXXXXXX/b/foo', ':p:~:.'))
call chdir(cwd)
call delete($HOME . '/XXXXXXXX', 'rf')
call assert_equal('''abc def''', fnamemodify('abc def', ':S'))
call assert_equal('''abc" "def''', fnamemodify('abc" "def', ':S'))
call assert_equal('''abc"%"def''', fnamemodify('abc"%"def', ':S'))
@ -44,6 +55,7 @@ func Test_fnamemodify()
let $HOME = save_home
let &shell = save_shell
let &shellslash = save_shellslash
endfunc
func Test_fnamemodify_er()