]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1507: symlinks are resolved on :cd commands v9.1.1507
authorglepnir <glephunter@gmail.com>
Thu, 3 Jul 2025 18:41:23 +0000 (20:41 +0200)
committerChristian Brabandt <cb@256bit.org>
Thu, 3 Jul 2025 18:44:50 +0000 (20:44 +0200)
Problem:  File paths change from symlink to target path after :cd command
          when editing files through symbolic links
Solution: Add "~" flag to 'cpoptions' to control symlink resolution.
          When not included (default), symlinks are resolved maintaining
          backward compatibility. When included, symlinks are preserved
          providing the improved behavior. (glepnir)

related: neovim/neovim#15695
closes: #17628

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/options.txt
runtime/doc/tags
runtime/doc/version9.txt
src/ex_docmd.c
src/option.h
src/testdir/test_cd.vim
src/testdir/test_options.vim
src/version.c

index b3c5697cfcb3f8c42d8ab3fd0f06a88ada216dec..cf064e17fd73b63275247c33ad4c01dd664e3d6d 100644 (file)
@@ -1,4 +1,4 @@
-*options.txt*  For Vim version 9.1.  Last change: 2025 Jul 01
+*options.txt*  For Vim version 9.1.  Last change: 2025 Jul 03
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -2390,7 +2390,7 @@ A jump table for the options with a short description can be found at |Q_op|.
 
                                                *'cpoptions'* *'cpo'* *cpo*
 'cpoptions' 'cpo'      string  (Vim default: "aABceFsz",
-                                Vi default:  all flags, except "#{|&/\."
+                                Vi default:  all flags, except "#{|&/\.~"
                                 |$VIM_POSIX|:  all flags)
                        global
        A sequence of single character flags.  When a character is present
@@ -2680,6 +2680,13 @@ A jump table for the options with a short description can be found at |Q_op|.
                        character, the cursor won't move. When not included,
                        the cursor would skip over it and jump to the
                        following occurrence.
+                                                               *cpo-~*
+               ~       When included, don't resolve symbolic links when
+                       changing directory with |:cd|, |:lcd|, or |:tcd|.
+                       This preserves the symbolic link path in buffer names
+                       and when displaying the current directory.  When
+                       excluded (default), symbolic links are resolved to
+                       their target paths.
 
        POSIX flags.  These are not included in the Vi default value, except
        when $VIM_POSIX was set on startup. |posix|
index 71a961697b2ddb0e171de39856b4e0c5d8766f07..6b363dbaa9d38a72338b272152522c8e59378f24 100644 (file)
@@ -6795,6 +6795,7 @@ cpo-x     options.txt     /*cpo-x*
 cpo-y  options.txt     /*cpo-y*
 cpo-z  options.txt     /*cpo-z*
 cpo-{  options.txt     /*cpo-{*
+cpo-~  options.txt     /*cpo-~*
 cpp.vim        syntax.txt      /*cpp.vim*
 crash-recovery recover.txt     /*crash-recovery*
 creating-menus gui.txt /*creating-menus*
index fcd3b9f3439511d0972bd4f74ff66fd0513fe50e..75d14011ab19261ea5604aa00d1b397de89c5d0b 100644 (file)
@@ -41643,6 +41643,8 @@ Options: ~
 - 'completeopt' is now a |global-local| option.
 - add 'cpoptions' flag "z" |cpo-z|, to disable some (traditional) vi
   behaviour/inconsistency (see |d-special| and |cw|).
+- add 'cpoptions' flag "~" |cpo-~| to disable resolving symlinks on |:cd|
+  commands
 - new option values for 'fillchars':
        "trunc"         - configure truncation indicator, 'pummaxwidth'
        "truncrl"       - like "trunc" but in 'rl' mode, 'pummaxwidth'
index ba0bff46687a0dc95043173333c422eea6e2d0f1..b985e2ba2bb8a5d856822b9579feab000f22d445 100644 (file)
@@ -8029,7 +8029,7 @@ post_chdir(cdscope_T scope)
     }
 
     last_chdir_reason = NULL;
-    shorten_fnames(TRUE);
+    shorten_fnames(vim_strchr(p_cpo, CPO_NOSYMLINKS) == NULL);
 }
 
 /*
index 4ed117c705edde5fa57d4446695d0e485e4d3eda..5590e5635bc31b8c09c8bc412c882aece76d80a6 100644 (file)
@@ -231,10 +231,11 @@ typedef enum {
 #define CPO_CHDIR      '.'     // don't chdir if buffer is modified
 #define CPO_SCOLON     ';'     // using "," and ";" will skip over char if
                                // cursor would not move
+#define CPO_NOSYMLINKS '~'     // don't resolve symlinks when changing directory
 // default values for Vim, Vi and POSIX
 #define CPO_VIM                "aABceFsz"
 #define CPO_VI         "aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>;"
-#define CPO_ALL                "aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\\.;"
+#define CPO_ALL                "aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\\.;~"
 
 // characters for p_ww option:
 #define WW_ALL         "bshl<>[]~"
index 13a3eba3707e0603a8e52371d3e618be3f9deeec..85494d386aeab6fc4513a434e8c85f53b7aff872 100644 (file)
@@ -252,4 +252,67 @@ func Test_getcwd_actual_dir()
   call chdir(startdir)
 endfunc
 
+func Test_cd_preserve_symlinks()
+  " Test new behavior: preserve symlinks when cpo-=~
+  set cpoptions+=~
+
+  let savedir = getcwd()
+  call mkdir('Xsource', 'R')
+  call writefile(['abc'], 'Xsource/foo.txt', 'D')
+
+  if has("win32")
+    silent !mklink /D Xdest Xsource
+  else
+    silent !ln -s Xsource Xdest
+  endif
+  if v:shell_error
+    call delete('Xsource', 'rf')
+    throw 'Skipped: cannot create symlinks'
+  endif
+
+  edit Xdest/foo.txt
+  let path_before = expand('%')
+  call assert_match('Xdest[/\\]foo\.txt$', path_before)
+
+  cd .
+  let path_after = expand('%')
+  call assert_equal(path_before, path_after)
+  call assert_match('Xdest[/\\]foo\.txt$', path_after)
+
+  bwipe!
+  set cpoptions&
+  call delete('Xdest', 'rf')
+  call delete('Xsource', 'rf')
+  call chdir(savedir)
+endfunc
+
+func Test_cd_symlinks()
+  CheckNotMSWindows
+
+  let savedir = getcwd()
+  call mkdir('Xsource', 'R')
+  call writefile(['abc'], 'Xsource/foo.txt', 'D')
+
+  silent !ln -s Xsource Xdest
+  if v:shell_error
+    call delete('Xsource', 'rf')
+    throw 'Skipped: cannot create symlinks'
+  endif
+
+  edit Xdest/foo.txt
+  let path_before = expand('%')
+  call assert_match('Xdest[/\\]foo\.txt$', path_before)
+
+  cd .
+  let path_after = expand('%')
+  call assert_match('Xsource[/\\]foo\.txt$', path_after)
+  call assert_notequal(path_before, path_after)
+
+  bwipe!
+  set cpoptions&
+  call delete('Xdest', 'rf')
+  call delete('Xsource', 'rf')
+  call chdir(savedir)
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index cb5636ee1d8ffb9e5cadae1bd4323db2ad54e0d7..21e4a03579b77c359be5870d05c798d39da8458f 100644 (file)
@@ -2273,7 +2273,7 @@ func Test_VIM_POSIX()
     qall
   [CODE]
   if RunVim([], after, '')
-    call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\.;',
+    call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\.;~',
           \            'AS'], readfile('X_VIM_POSIX'))
   endif
 
@@ -2528,7 +2528,7 @@ func Test_string_option_revert_on_failure()
         \ ['completeopt', 'popup', 'a123'],
         \ ['completepopup', 'width:20', 'border'],
         \ ['concealcursor', 'v', 'xyz'],
-        \ ['cpoptions', 'HJ', '~'],
+        \ ['cpoptions', 'HJ', 'Q'],
         \ ['cryptmethod', 'zip', 'a123'],
         \ ['cursorlineopt', 'screenline', 'a123'],
         \ ['debug', 'throw', 'a123'],
index d5a882da6bb6062d437cc0148b58f970d1c6ee23..9d27681688ab30953492c9873df6a9ba03614bc2 100644 (file)
@@ -719,6 +719,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1507,
 /**/
     1506,
 /**/