]> granicus.if.org Git - vim/commitdiff
patch 8.2.3920: restoring directory after using another window is inefficient v8.2.3920
authorBram Moolenaar <Bram@vim.org>
Tue, 28 Dec 2021 13:15:05 +0000 (13:15 +0000)
committerBram Moolenaar <Bram@vim.org>
Tue, 28 Dec 2021 13:15:05 +0000 (13:15 +0000)
Problem:    Restoring directory after using another window is inefficient.
Solution:   Only restore the directory for win_execute().  Apply 'autochdir'
            only when needed.

src/evalwindow.c
src/testdir/test_autochdir.vim
src/version.c

index 794d6aa0bb2a6a976cb3aea616a69dd8409ecc04..ebab856142435248faf1631b5c24a1f942d4cd74 100644 (file)
@@ -707,6 +707,21 @@ f_win_execute(typval_T *argvars, typval_T *rettv)
     if (wp != NULL && tp != NULL)
     {
        pos_T   curpos = wp->w_cursor;
+       char_u  cwd[MAXPATHL];
+       int     cwd_status;
+       char_u  autocwd[MAXPATHL];
+       int     apply_acd = FALSE;
+
+       cwd_status = mch_dirname(cwd, MAXPATHL);
+
+       // If 'acd' is set, check we are using that directory.  If yes, then
+       // apply 'acd' afterwards, otherwise restore the current directory.
+       if (cwd_status == OK && p_acd)
+       {
+           do_autochdir();
+           apply_acd = mch_dirname(autocwd, MAXPATHL) == OK
+                                                 && STRCMP(cwd, autocwd) == 0;
+       }
 
        if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
        {
@@ -714,6 +729,10 @@ f_win_execute(typval_T *argvars, typval_T *rettv)
            execute_common(argvars, rettv, 1);
        }
        restore_win_noblock(save_curwin, save_curtab, TRUE);
+       if (apply_acd)
+           do_autochdir();
+       else if (cwd_status == OK)
+           mch_chdir((char *)cwd);
 
        // Update the status line if the cursor moved.
        if (win_valid(wp) && !EQUAL_POS(curpos, wp->w_cursor))
@@ -1316,9 +1335,5 @@ restore_win_noblock(
        // to the first valid window.
        win_goto(firstwin);
 # endif
-
-    // If called by win_execute() and executing the command changed the
-    // directory, it now has to be restored.
-    fix_current_dir();
 }
 #endif
index 861db7247a63187454d2d96e7a198989f4b47c5a..5d367757fcc4d7d738fb501561f526b231ba3af5 100644 (file)
@@ -51,6 +51,26 @@ func Test_set_filename_other_window()
   endtry
 endfunc
 
+func Test_acd_win_execute()
+  let cwd = getcwd()
+  set acd
+  call test_autochdir()
+
+  call mkdir('Xfile')
+  let winid = win_getid()
+  new Xfile/file
+  call assert_match('testdir.Xfile$', getcwd())
+  cd ..
+  call assert_match('testdir$', getcwd())
+  call win_execute(winid, 'echo')
+  call assert_match('testdir$', getcwd())
+
+  bwipe!
+  set noacd
+  call chdir(cwd)
+  call delete('Xfile', 'rf')
+endfunc
+
 func Test_verbose_pwd()
   let cwd = getcwd()
   call test_autochdir()
index 2045de932dc1595f8cebdd7bbd198ff2e51fdd98..8963062fcd150424fb29076287e531fc2e0f38bc 100644 (file)
@@ -749,6 +749,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3920,
 /**/
     3919,
 /**/