Patch 8.2.4335
Problem:    No autocommand event triggered before changing directory. (Ronnie
            Magatti)
Solution:   Add DirChangedPre. (closes #9721)
Files:      runtime/doc/autocmd.txt, src/ex_docmd.c, src/proto/ex_docmd.pro,
            src/vim.h, src/autocmd.c, src/misc2.c,
            src/testdir/test_autocmd.vim


*** ../vim-8.2.4334/runtime/doc/autocmd.txt     2021-12-29 19:41:42.446404689 
+0000
--- runtime/doc/autocmd.txt     2022-02-09 12:17:23.622089964 +0000
***************
*** 326,331 ****
--- 326,332 ----
  |FileChangedRO|               before making the first change to a read-only 
file
  
  |DiffUpdated|         after diffs have been updated
+ |DirChangedPre|               before the working directory will change
  |DirChanged|          after the working directory has changed
  
  |ShellCmdPost|                after executing a shell command
***************
*** 735,740 ****
--- 739,749 ----
                                what kind of diff is being used (internal or
                                external) this can be triggered on every
                                change or when doing |:diffupdate|.
+                                                       *DirChangedPre*
+ DirChangedPre                 The working directory is going to be changed,
+                               as with ||DirChanged|.  The pattern is like
+                               with |DirChanged|.  The new directory can be
+                               found in v:event.directory.
                                                        *DirChanged*
  DirChanged                    The working directory has changed in response
                                to the |:cd| or |:tcd| or |:lcd| commands, or
*** ../vim-8.2.4334/src/ex_docmd.c      2022-02-02 13:16:33.144003948 +0000
--- src/ex_docmd.c      2022-02-09 12:48:40.051160073 +0000
***************
*** 7343,7348 ****
--- 7343,7368 ----
  }
  
  /*
+  * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
+  */
+     void
+ trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
+ {
+ #ifdef FEAT_EVAL
+     dict_T        *v_event;
+     save_v_event_T  save_v_event;
+ 
+     v_event = get_v_event(&save_v_event);
+     (void)dict_add_string(v_event, "directory", new_dir);
+     dict_set_items_ro(v_event);
+ #endif
+     apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
+ #ifdef FEAT_EVAL
+     restore_v_event(v_event, &save_v_event);
+ #endif
+ }
+ 
+ /*
   * Change directory function used by :cd/:tcd/:lcd Ex commands and the
   * chdir() function.
   * scope == CDSCOPE_WINDOW: changes the window-local directory
***************
*** 7358,7364 ****
  {
      char_u    *pdir = NULL;
      int               dir_differs;
!     char_u    *acmd_fname;
      char_u    **pp;
  
      if (new_dir == NULL || allbuf_locked())
--- 7378,7384 ----
  {
      char_u    *pdir = NULL;
      int               dir_differs;
!     char_u    *acmd_fname = NULL;
      char_u    **pp;
  
      if (new_dir == NULL || allbuf_locked())
***************
*** 7411,7422 ****
        new_dir = NameBuff;
      }
      dir_differs = pdir == NULL
!       || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
!     if (dir_differs && vim_chdir(new_dir))
      {
!       emsg(_(e_command_failed));
!       vim_free(pdir);
!       return FALSE;
      }
  
      if (scope == CDSCOPE_WINDOW)
--- 7431,7453 ----
        new_dir = NameBuff;
      }
      dir_differs = pdir == NULL
!                           || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
!     if (dir_differs)
      {
!       if (scope == CDSCOPE_WINDOW)
!           acmd_fname = (char_u *)"window";
!       else if (scope == CDSCOPE_TABPAGE)
!           acmd_fname = (char_u *)"tabpage";
!       else
!           acmd_fname = (char_u *)"global";
!       trigger_DirChangedPre(acmd_fname, new_dir);
! 
!       if (vim_chdir(new_dir))
!       {
!           emsg(_(e_command_failed));
!           vim_free(pdir);
!           return FALSE;
!       }
      }
  
      if (scope == CDSCOPE_WINDOW)
***************
*** 7431,7446 ****
      post_chdir(scope);
  
      if (dir_differs)
!     {
!       if (scope == CDSCOPE_WINDOW)
!           acmd_fname = (char_u *)"window";
!       else if (scope == CDSCOPE_TABPAGE)
!           acmd_fname = (char_u *)"tabpage";
!       else
!           acmd_fname = (char_u *)"global";
!       apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
!                                                           curbuf);
!     }
      return TRUE;
  }
  
--- 7462,7468 ----
      post_chdir(scope);
  
      if (dir_differs)
!       apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
      return TRUE;
  }
  
*** ../vim-8.2.4334/src/proto/ex_docmd.pro      2021-12-29 19:41:42.446404689 
+0000
--- src/proto/ex_docmd.pro      2022-02-09 12:48:27.087180386 +0000
***************
*** 48,53 ****
--- 48,54 ----
  void do_exedit(exarg_T *eap, win_T *old_curwin);
  void free_cd_dir(void);
  void post_chdir(cdscope_T scope);
+ void trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir);
  int changedir_func(char_u *new_dir, int forceit, cdscope_T scope);
  void ex_cd(exarg_T *eap);
  void do_sleep(long msec, int hide_cursor);
*** ../vim-8.2.4334/src/vim.h   2022-02-08 12:07:41.831496906 +0000
--- src/vim.h   2022-02-09 12:21:18.321728000 +0000
***************
*** 1304,1309 ****
--- 1304,1310 ----
      EVENT_CURSORMOVEDI,               // cursor was moved in Insert mode
      EVENT_DIFFUPDATED,                // after diffs were updated
      EVENT_DIRCHANGED,         // after user changed directory
+     EVENT_DIRCHANGEDPRE,      // before directory changes
      EVENT_ENCODINGCHANGED,    // after changing the 'encoding' option
      EVENT_EXITPRE,            // before exiting
      EVENT_FILEAPPENDCMD,      // append to a file using command
*** ../vim-8.2.4334/src/autocmd.c       2022-01-24 20:00:51.899507349 +0000
--- src/autocmd.c       2022-02-09 12:39:15.132049327 +0000
***************
*** 119,124 ****
--- 119,125 ----
      {"CursorMovedI",  EVENT_CURSORMOVEDI},
      {"DiffUpdated",   EVENT_DIFFUPDATED},
      {"DirChanged",    EVENT_DIRCHANGED},
+     {"DirChangedPre", EVENT_DIRCHANGEDPRE},
      {"EncodingChanged",       EVENT_ENCODINGCHANGED},
      {"ExitPre",               EVENT_EXITPRE},
      {"FileEncoding",  EVENT_ENCODINGCHANGED},
*** ../vim-8.2.4334/src/misc2.c 2022-02-02 13:16:33.144003948 +0000
--- src/misc2.c 2022-02-09 12:48:54.007138203 +0000
***************
*** 1914,1919 ****
--- 1914,1922 ----
        // nothing to do
        return OK;
  
+     if (trigger_autocmd != NULL)
+       trigger_DirChangedPre((char_u *)trigger_autocmd, new_dir);
+ 
      if (mch_chdir((char *)new_dir) != 0)
        return FAIL;
  
*** ../vim-8.2.4334/src/testdir/test_autocmd.vim        2022-01-20 
11:17:14.548366296 +0000
--- src/testdir/test_autocmd.vim        2022-02-09 12:43:01.259692206 +0000
***************
*** 1922,1935 ****
  
  function Test_dirchanged_global()
    call s:Before_test_dirchanged()
    autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
    autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
    call chdir(s:dir_foo)
!   call assert_equal(["cd:", s:dir_foo], s:li)
    call chdir(s:dir_foo)
!   call assert_equal(["cd:", s:dir_foo], s:li)
    exe 'lcd ' .. fnameescape(s:dir_bar)
!   call assert_equal(["cd:", s:dir_foo], s:li)
    call s:After_test_dirchanged()
  endfunc
  
--- 1922,1937 ----
  
  function Test_dirchanged_global()
    call s:Before_test_dirchanged()
+   autocmd test_dirchanged DirChangedPre global call add(s:li, "pre cd " .. 
v:event.directory)
    autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
    autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
    call chdir(s:dir_foo)
!   let expected = ["pre cd " .. s:dir_foo, "cd:", s:dir_foo]
!   call assert_equal(expected, s:li)
    call chdir(s:dir_foo)
!   call assert_equal(expected, s:li)
    exe 'lcd ' .. fnameescape(s:dir_bar)
!   call assert_equal(expected, s:li)
    call s:After_test_dirchanged()
  endfunc
  
***************
*** 1950,1955 ****
--- 1952,1958 ----
    CheckOption autochdir
    call s:Before_test_dirchanged()
    call test_autochdir()
+   autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. 
v:event.directory)
    autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
    autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
    set acd
***************
*** 1957,1963 ****
    call assert_equal([], s:li)
    exe 'edit ' . s:dir_foo . '/Xfile'
    call assert_equal(s:dir_foo, getcwd())
!   call assert_equal(["auto:", s:dir_foo], s:li)
    set noacd
    bwipe!
    call s:After_test_dirchanged()
--- 1960,1967 ----
    call assert_equal([], s:li)
    exe 'edit ' . s:dir_foo . '/Xfile'
    call assert_equal(s:dir_foo, getcwd())
!   let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
!   call assert_equal(expected, s:li)
    set noacd
    bwipe!
    call s:After_test_dirchanged()
*** ../vim-8.2.4334/src/version.c       2022-02-09 11:55:28.004059225 +0000
--- src/version.c       2022-02-09 12:49:30.019081825 +0000
***************
*** 748,749 ****
--- 748,751 ----
  {   /* Add new patch number below this line */
+ /**/
+     4335,
  /**/

-- 
How To Keep A Healthy Level Of Insanity:
7. Finish all your sentences with "in accordance with the prophecy".

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220209130013.7C7A51C1905%40moolenaar.net.

Raspunde prin e-mail lui