diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index bd5288a0e3..78aa02fc3c 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -421,6 +421,8 @@ These existing features changed their behavior. • 'scrollback' maximum value increased from 100000 to 1000000 • |matchfuzzy()| and |matchfuzzypos()| use an improved fuzzy matching algorithm (same as fzy). +- Windows: Paths like "\Windows" and "/Windows" are now considered to be + absolute paths (to the current drive) and no longer relative. ============================================================================== REMOVED FEATURES *news-removed* diff --git a/src/nvim/eval/fs.c b/src/nvim/eval/fs.c index 2b97cebde0..65e27b0620 100644 --- a/src/nvim/eval/fs.c +++ b/src/nvim/eval/fs.c @@ -111,7 +111,11 @@ repeat: } // FullName_save() is slow, don't use it when not needed. - if (*p != NUL || !vim_isAbsName(*fnamep)) { + if (*p != NUL || !vim_isAbsName(*fnamep) +#ifdef MSWIN // enforce drive letter on Windows paths + || **fnamep == '/' || **fnamep == '\\' +#endif + ) { *fnamep = FullName_save(*fnamep, *p != NUL); xfree(*bufp); // free any allocated file name *bufp = *fnamep; diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c index 15658e74e2..bc97b2c482 100644 --- a/src/nvim/file_search.c +++ b/src/nvim/file_search.c @@ -331,17 +331,6 @@ void *vim_findfile_init(char *path, char *filename, size_t filenamelen, char *st ff_expand_buffer.size = strlen(ff_expand_buffer.data); search_ctx->ffsc_start_dir = copy_string(ff_expand_buffer, NULL); - -#ifdef BACKSLASH_IN_FILENAME - // A path that starts with "/dir" is relative to the drive, not to the - // directory (but not for "//machine/dir"). Only use the drive name. - if ((*path == '/' || *path == '\\') - && path[1] != path[0] - && search_ctx->ffsc_start_dir.data[1] == ':') { - search_ctx->ffsc_start_dir.data[2] = NUL; - search_ctx->ffsc_start_dir.size = 2; - } -#endif } // If stopdirs are given, split them into an array of pointers. diff --git a/src/nvim/path.c b/src/nvim/path.c index 42f4d479da..9d4c8d7bde 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -5,6 +5,9 @@ #include #include #include +#ifdef MSWIN +# include +#endif #include "auto/config.h" #include "nvim/ascii_defs.h" @@ -376,6 +379,38 @@ int path_fnamencmp(const char *const fname1, const char *const fname2, size_t le const char *p1 = fname1; const char *p2 = fname2; + +# ifdef MSWIN + // To allow proper comparison of absolute paths: + // - one with explicit drive letter C:\xxx + // - another with implicit drive letter \xxx + // advance the pointer, of the explicit one, to skip the drive + for (int swap = 0, drive = NUL; swap < 2; swap++) { + // Handle absolute paths with implicit drive letter + c1 = utf_ptr2char(p1); + c2 = utf_ptr2char(p2); + + if ((c1 == '/' || c1 == '\\') && ASCII_ISALPHA(c2)) { + drive = mb_toupper(c2) - 'A' + 1; + + // Check for the colon + p2 += utfc_ptr2len(p2); + c2 = utf_ptr2char(p2); + if (c2 == ':' && drive == _getdrive()) { // skip the drive for comparison + p2 += utfc_ptr2len(p2); + break; + } else { // ignore + p2 -= utfc_ptr2len(p2); + } + } + + // swap pointers + const char *tmp = p1; + p1 = p2; + p2 = tmp; + } +# endif + while (len > 0) { c1 = utf_ptr2char(p1); c2 = utf_ptr2char(p2); @@ -1834,7 +1869,7 @@ int vim_FullName(const char *fname, char *buf, size_t len, bool force) /// the root may have relative paths (like dir/../subdir) or symlinks /// embedded, or even extra separators (//). This function addresses /// those possibilities, returning a resolved absolute path. -/// For MS-Windows, this also expands names like "longna~1". +/// For MS-Windows, this also provides drive letter for all absolute paths. /// /// @param fname is the filename to expand /// @return [allocated] Full path (NULL for failure). @@ -1848,6 +1883,10 @@ char *fix_fname(const char *fname) || strstr(fname, "//") != NULL # ifdef BACKSLASH_IN_FILENAME || strstr(fname, "\\\\") != NULL +# endif +# ifdef MSWIN + || fname[0] == '/' + || fname[0] == '\\' # endif ) { return FullName_save(fname, false); @@ -2328,7 +2367,11 @@ static int path_to_absolute(const char *fname, char *buf, size_t len, int force) const char *end_of_path = fname; // expand it if forced or not an absolute path - if (force || !path_is_absolute(fname)) { + if (force || !path_is_absolute(fname) +#ifdef MSWIN // enforce drive letter on Windows paths + || fname[0] == '/' || fname[0] == '\\' +#endif + ) { p = strrchr(fname, '/'); #ifdef MSWIN if (p == NULL) { @@ -2372,8 +2415,9 @@ bool path_is_absolute(const char *fname) return false; } // A name like "d:/foo" and "//server/share" is absolute - return ((isalpha((uint8_t)fname[0]) && fname[1] == ':' && vim_ispathsep_nocolon(fname[2])) - || (vim_ispathsep_nocolon(fname[0]) && fname[0] == fname[1])); + // /foo and \foo are absolute too because Windows keeps a current drive. + return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':' && vim_ispathsep_nocolon(fname[2])) + || vim_ispathsep_nocolon(fname[0])); #else // UNIX: This just checks if the file name starts with '/' or '~'. return *fname == '/' || *fname == '~'; diff --git a/test/old/testdir/test_cd.vim b/test/old/testdir/test_cd.vim index 25e95ac4f6..1b0ef9ef81 100644 --- a/test/old/testdir/test_cd.vim +++ b/test/old/testdir/test_cd.vim @@ -224,6 +224,66 @@ func Test_cd_completion() call assert_equal('"' .. cmd .. ' XComplDir1/ XComplDir2/ XComplDir3/', @:) endfor set cdpath& + + if has('win32') + " Test Windows absolute path completion + let saved_cwd = getcwd() + + " Retrieve a suitable dir in the current drive + for d in readdir('/', 'isdirectory("/" .. v:val) && len(v:val) > 2') + " Paths containing '$' such as "$RECYCLE.BIN" are skipped because + " they are considered environment variables and completion does not + " work. + if d =~ '\V$' + continue + endif + " Skip directories that we don't have permission to "cd" into by + " actually "cd"ing into them and making sure they don't fail. + " Directory "System Volume Information" is an example of this. + try + call chdir('/' .. d) + let dir = d + " Yay! We found a suitable dir! + break + catch /:E472:/ + " Just skip directories where "cd" fails + continue + finally + call chdir(saved_cwd) + endtry + endfor + if !exists('dir') + throw 'Skipped: no testable directories found in the current drive root' + endif + + " Get partial path + let partial = dir[0:-2] + " Get the current drive letter and full path of the target dir + call chdir('/' .. dir) + let full = getcwd() + let drive = full[0] + call chdir(saved_cwd) + + for cmd in ['cd', 'chdir', 'lcd', 'lchdir', 'tcd', 'tchdir'] + for sep in [ '/', '\'] + + " Explicit drive letter + call feedkeys(':' .. cmd .. ' ' .. drive .. ':' .. sep .. + \ partial .. "\\\"\", 'tx') + call assert_match(full, @:) + + " Implicit drive letter + call feedkeys(':' .. cmd .. ' ' .. sep .. partial .. "\\\"\", 'tx') + call assert_match('/' .. dir .. '/', @:) + + " UNC path + call feedkeys(':' .. cmd .. ' ' .. sep .. sep .. $COMPUTERNAME .. sep .. + \ drive .. '$' .. sep .. partial .."\\\"\", 'tx') + call assert_match('//' .. $COMPUTERNAME .. '/' .. drive .. '$/' .. dir .. '/' , @:) + + endfor + endfor + endif endfunc func Test_cd_unknown_dir() diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim index d90f2f6102..b67c38349b 100644 --- a/test/old/testdir/test_functions.vim +++ b/test/old/testdir/test_functions.vim @@ -3735,7 +3735,8 @@ func Test_isabsolutepath() call assert_true(isabsolutepath('A:\Foo')) call assert_true(isabsolutepath('A:/Foo')) call assert_false(isabsolutepath('A:Foo')) - call assert_false(isabsolutepath('\Windows')) + call assert_true(isabsolutepath('\Windows')) + call assert_true(isabsolutepath('/Windows')) call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt')) else call assert_true(isabsolutepath('/'))