65512b0
To: vim-dev@vim.org
65512b0
Subject: patch 7.1.126
65512b0
Fcc: outbox
65512b0
From: Bram Moolenaar <Bram@moolenaar.net>
65512b0
Mime-Version: 1.0
65512b0
Content-Type: text/plain; charset=ISO-8859-1
65512b0
Content-Transfer-Encoding: 8bit
65512b0
------------
65512b0
65512b0
Patch 7.1.126
65512b0
Problem:    ":vimgrep */*" fails when a BufRead autocommand changes directory.
65512b0
	    (Bernhard Kuhn)
65512b0
Solution:   Change back to the original directory after loading a file.
65512b0
	    Also: use shorten_fname1() to avoid duplicating code.
65512b0
Files:	    src/buffer.c, src/ex_docmd.c, src/fileio.c, src/gui_gtk.c,
65512b0
	    src/gui_w48.c, src/proto/ex_docmd.pro, src/proto/fileio.pro,
65512b0
	    src/quickfix.c
65512b0
65512b0
65512b0
*** ../vim-7.1.125/src/buffer.c	Sat Sep 29 14:15:00 2007
65512b0
--- src/buffer.c	Wed Sep 26 20:05:38 2007
65512b0
***************
65512b0
*** 4261,4272 ****
65512b0
  do_arg_all(count, forceit, keep_tabs)
65512b0
      int	count;
65512b0
      int	forceit;		/* hide buffers in current windows */
65512b0
!     int keep_tabs;		/* keep curren tabs, for ":tab drop file" */
65512b0
  {
65512b0
      int		i;
65512b0
      win_T	*wp, *wpnext;
65512b0
      char_u	*opened;	/* array of flags for which args are open */
65512b0
!     int		opened_len;	/* lenght of opened[] */
65512b0
      int		use_firstwin = FALSE;	/* use first window for arglist */
65512b0
      int		split_ret = OK;
65512b0
      int		p_ea_save;
65512b0
--- 4261,4272 ----
65512b0
  do_arg_all(count, forceit, keep_tabs)
65512b0
      int	count;
65512b0
      int	forceit;		/* hide buffers in current windows */
65512b0
!     int keep_tabs;		/* keep current tabs, for ":tab drop file" */
65512b0
  {
65512b0
      int		i;
65512b0
      win_T	*wp, *wpnext;
65512b0
      char_u	*opened;	/* array of flags for which args are open */
65512b0
!     int		opened_len;	/* length of opened[] */
65512b0
      int		use_firstwin = FALSE;	/* use first window for arglist */
65512b0
      int		split_ret = OK;
65512b0
      int		p_ea_save;
65512b0
***************
65512b0
*** 4946,4955 ****
65512b0
  	/* Expand "~/" in the file name at "line + 1" to a full path.
65512b0
  	 * Then try shortening it by comparing with the current directory */
65512b0
  	expand_env(xline, NameBuff, MAXPATHL);
65512b0
! 	mch_dirname(IObuff, IOSIZE);
65512b0
! 	sfname = shorten_fname(NameBuff, IObuff);
65512b0
! 	if (sfname == NULL)
65512b0
! 	    sfname = NameBuff;
65512b0
  
65512b0
  	buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
65512b0
  	if (buf != NULL)	/* just in case... */
65512b0
--- 4946,4952 ----
65512b0
  	/* Expand "~/" in the file name at "line + 1" to a full path.
65512b0
  	 * Then try shortening it by comparing with the current directory */
65512b0
  	expand_env(xline, NameBuff, MAXPATHL);
65512b0
! 	sfname = shorten_fname1(NameBuff);
65512b0
  
65512b0
  	buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
65512b0
  	if (buf != NULL)	/* just in case... */
65512b0
*** ../vim-7.1.125/src/ex_docmd.c	Wed Sep 26 22:35:06 2007
65512b0
--- src/ex_docmd.c	Wed Sep 26 20:29:36 2007
65512b0
***************
65512b0
*** 276,282 ****
65512b0
  static void	ex_swapname __ARGS((exarg_T *eap));
65512b0
  static void	ex_syncbind __ARGS((exarg_T *eap));
65512b0
  static void	ex_read __ARGS((exarg_T *eap));
65512b0
- static void	ex_cd __ARGS((exarg_T *eap));
65512b0
  static void	ex_pwd __ARGS((exarg_T *eap));
65512b0
  static void	ex_equal __ARGS((exarg_T *eap));
65512b0
  static void	ex_sleep __ARGS((exarg_T *eap));
65512b0
--- 276,281 ----
65512b0
***************
65512b0
*** 7778,7784 ****
65512b0
  /*
65512b0
   * ":cd", ":lcd", ":chdir" and ":lchdir".
65512b0
   */
65512b0
!     static void
65512b0
  ex_cd(eap)
65512b0
      exarg_T	*eap;
65512b0
  {
65512b0
--- 7777,7783 ----
65512b0
  /*
65512b0
   * ":cd", ":lcd", ":chdir" and ":lchdir".
65512b0
   */
65512b0
!     void
65512b0
  ex_cd(eap)
65512b0
      exarg_T	*eap;
65512b0
  {
65512b0
*** ../vim-7.1.125/src/fileio.c	Sat Sep 29 14:15:00 2007
65512b0
--- src/fileio.c	Wed Sep 26 20:02:54 2007
65512b0
***************
65512b0
*** 114,120 ****
65512b0
  {
65512b0
      int		bw_fd;		/* file descriptor */
65512b0
      char_u	*bw_buf;	/* buffer with data to be written */
65512b0
!     int		bw_len;	/* lenght of data */
65512b0
  #ifdef HAS_BW_FLAGS
65512b0
      int		bw_flags;	/* FIO_ flags */
65512b0
  #endif
65512b0
--- 114,120 ----
65512b0
  {
65512b0
      int		bw_fd;		/* file descriptor */
65512b0
      char_u	*bw_buf;	/* buffer with data to be written */
65512b0
!     int		bw_len;		/* length of data */
65512b0
  #ifdef HAS_BW_FLAGS
65512b0
      int		bw_flags;	/* FIO_ flags */
65512b0
  #endif
65512b0
***************
65512b0
*** 5552,5557 ****
65512b0
--- 5553,5579 ----
65512b0
      return (int)(p - buf);
65512b0
  }
65512b0
  #endif
65512b0
+ 
65512b0
+ /*
65512b0
+  * Try to find a shortname by comparing the fullname with the current
65512b0
+  * directory.
65512b0
+  * Returns "full_path" or pointer into "full_path" if shortened.
65512b0
+  */
65512b0
+     char_u *
65512b0
+ shorten_fname1(full_path)
65512b0
+     char_u	*full_path;
65512b0
+ {
65512b0
+     char_u	dirname[MAXPATHL];
65512b0
+     char_u	*p = full_path;
65512b0
+ 
65512b0
+     if (mch_dirname(dirname, MAXPATHL) == OK)
65512b0
+     {
65512b0
+ 	p = shorten_fname(full_path, dirname);
65512b0
+ 	if (p == NULL || *p == NUL)
65512b0
+ 	    p = full_path;
65512b0
+     }
65512b0
+     return p;
65512b0
+ }
65512b0
  
65512b0
  /*
65512b0
   * Try to find a shortname by comparing the fullname with the current
65512b0
*** ../vim-7.1.125/src/gui_gtk.c	Tue Aug 14 14:59:41 2007
65512b0
--- src/gui_gtk.c	Wed Sep 26 20:07:58 2007
65512b0
***************
65512b0
*** 1272,1278 ****
65512b0
      GtkWidget		*fc;
65512b0
  #endif
65512b0
      char_u		dirbuf[MAXPATHL];
65512b0
-     char_u		*p;
65512b0
  
65512b0
  # ifdef HAVE_GTK2
65512b0
      title = CONVERT_TO_UTF8(title);
65512b0
--- 1272,1277 ----
65512b0
***************
65512b0
*** 1363,1373 ****
65512b0
  	return NULL;
65512b0
  
65512b0
      /* shorten the file name if possible */
65512b0
!     mch_dirname(dirbuf, MAXPATHL);
65512b0
!     p = shorten_fname(gui.browse_fname, dirbuf);
65512b0
!     if (p == NULL)
65512b0
! 	p = gui.browse_fname;
65512b0
!     return vim_strsave(p);
65512b0
  }
65512b0
  
65512b0
  #if defined(HAVE_GTK2) || defined(PROTO)
65512b0
--- 1362,1368 ----
65512b0
  	return NULL;
65512b0
  
65512b0
      /* shorten the file name if possible */
65512b0
!     return vim_strsave(shorten_fname1(gui.browse_fname));
65512b0
  }
65512b0
  
65512b0
  #if defined(HAVE_GTK2) || defined(PROTO)
65512b0
***************
65512b0
*** 1427,1437 ****
65512b0
  	return NULL;
65512b0
  
65512b0
      /* shorten the file name if possible */
65512b0
!     mch_dirname(dirbuf, MAXPATHL);
65512b0
!     p = shorten_fname(dirname, dirbuf);
65512b0
!     if (p == NULL || *p == NUL)
65512b0
! 	p = dirname;
65512b0
!     p = vim_strsave(p);
65512b0
      g_free(dirname);
65512b0
      return p;
65512b0
  
65512b0
--- 1422,1428 ----
65512b0
  	return NULL;
65512b0
  
65512b0
      /* shorten the file name if possible */
65512b0
!     p = vim_strsave(shorten_fname1(dirname));
65512b0
      g_free(dirname);
65512b0
      return p;
65512b0
  
65512b0
*** ../vim-7.1.125/src/gui_w48.c	Thu May 10 19:17:07 2007
65512b0
--- src/gui_w48.c	Wed Sep 26 20:09:33 2007
65512b0
***************
65512b0
*** 3301,3311 ****
65512b0
      SetFocus(s_hwnd);
65512b0
  
65512b0
      /* Shorten the file name if possible */
65512b0
!     mch_dirname(IObuff, IOSIZE);
65512b0
!     p = shorten_fname((char_u *)fileBuf, IObuff);
65512b0
!     if (p == NULL)
65512b0
! 	p = (char_u *)fileBuf;
65512b0
!     return vim_strsave(p);
65512b0
  }
65512b0
  # endif /* FEAT_MBYTE */
65512b0
  
65512b0
--- 3301,3307 ----
65512b0
      SetFocus(s_hwnd);
65512b0
  
65512b0
      /* Shorten the file name if possible */
65512b0
!     return vim_strsave(shorten_fname1((char_u *)fileBuf));
65512b0
  }
65512b0
  # endif /* FEAT_MBYTE */
65512b0
  
65512b0
***************
65512b0
*** 3450,3460 ****
65512b0
      SetFocus(s_hwnd);
65512b0
  
65512b0
      /* Shorten the file name if possible */
65512b0
!     mch_dirname(IObuff, IOSIZE);
65512b0
!     p = shorten_fname((char_u *)fileBuf, IObuff);
65512b0
!     if (p == NULL)
65512b0
! 	p = (char_u *)fileBuf;
65512b0
!     return vim_strsave(p);
65512b0
  }
65512b0
  #endif /* FEAT_BROWSE */
65512b0
  
65512b0
--- 3446,3452 ----
65512b0
      SetFocus(s_hwnd);
65512b0
  
65512b0
      /* Shorten the file name if possible */
65512b0
!     return vim_strsave(shorten_fname1((char_u *)fileBuf));
65512b0
  }
65512b0
  #endif /* FEAT_BROWSE */
65512b0
  
65512b0
*** ../vim-7.1.125/src/proto/ex_docmd.pro	Sun May  6 14:46:22 2007
65512b0
--- src/proto/ex_docmd.pro	Wed Sep 26 20:30:10 2007
65512b0
***************
65512b0
*** 39,44 ****
65512b0
--- 39,45 ----
65512b0
  void tabpage_new __ARGS((void));
65512b0
  void do_exedit __ARGS((exarg_T *eap, win_T *old_curwin));
65512b0
  void free_cd_dir __ARGS((void));
65512b0
+ void ex_cd __ARGS((exarg_T *eap));
65512b0
  void do_sleep __ARGS((long msec));
65512b0
  int vim_mkdir_emsg __ARGS((char_u *name, int prot));
65512b0
  FILE *open_exfile __ARGS((char_u *fname, int forceit, char *mode));
65512b0
*** ../vim-7.1.125/src/proto/fileio.pro	Sat Sep 29 14:15:00 2007
65512b0
--- src/proto/fileio.pro	Wed Sep 26 20:05:02 2007
65512b0
***************
65512b0
*** 6,11 ****
65512b0
--- 6,12 ----
65512b0
  int buf_write __ARGS((buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_T end, exarg_T *eap, int append, int forceit, int reset_changed, int filtering));
65512b0
  void msg_add_fname __ARGS((buf_T *buf, char_u *fname));
65512b0
  void msg_add_lines __ARGS((int insert_space, long lnum, long nchars));
65512b0
+ char_u *shorten_fname1 __ARGS((char_u *full_path));
65512b0
  char_u *shorten_fname __ARGS((char_u *full_path, char_u *dir_name));
65512b0
  void shorten_fnames __ARGS((int force));
65512b0
  void shorten_filenames __ARGS((char_u **fnames, int count));
65512b0
*** ../vim-7.1.125/src/quickfix.c	Sun Sep 16 13:26:56 2007
65512b0
--- src/quickfix.c	Sun Sep 30 13:58:38 2007
65512b0
***************
65512b0
*** 2972,2977 ****
65512b0
--- 2972,2978 ----
65512b0
      regmmatch_T	regmatch;
65512b0
      int		fcount;
65512b0
      char_u	**fnames;
65512b0
+     char_u	*fname;
65512b0
      char_u	*s;
65512b0
      char_u	*p;
65512b0
      int		fi;
65512b0
***************
65512b0
*** 2995,3000 ****
65512b0
--- 2996,3004 ----
65512b0
      int		flags = 0;
65512b0
      colnr_T	col;
65512b0
      long	tomatch;
65512b0
+     char_u	dirname_start[MAXPATHL];
65512b0
+     char_u	dirname_now[MAXPATHL];
65512b0
+     char_u	*target_dir = NULL;
65512b0
  
65512b0
      switch (eap->cmdidx)
65512b0
      {
65512b0
***************
65512b0
*** 3069,3085 ****
65512b0
  	goto theend;
65512b0
      }
65512b0
  
65512b0
      seconds = (time_t)0;
65512b0
      for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
65512b0
      {
65512b0
  	if (time(NULL) > seconds)
65512b0
  	{
65512b0
! 	    /* Display the file name every second or so. */
65512b0
  	    seconds = time(NULL);
65512b0
  	    msg_start();
65512b0
! 	    p = msg_strtrunc(fnames[fi], TRUE);
65512b0
  	    if (p == NULL)
65512b0
! 		msg_outtrans(fnames[fi]);
65512b0
  	    else
65512b0
  	    {
65512b0
  		msg_outtrans(p);
65512b0
--- 3073,3095 ----
65512b0
  	goto theend;
65512b0
      }
65512b0
  
65512b0
+     /* Remember the current directory, because a BufRead autocommand that does
65512b0
+      * ":lcd %:p:h" changes the meaning of short path names. */
65512b0
+     mch_dirname(dirname_start, MAXPATHL);
65512b0
+ 
65512b0
      seconds = (time_t)0;
65512b0
      for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
65512b0
      {
65512b0
+ 	fname = shorten_fname1(fnames[fi]);
65512b0
  	if (time(NULL) > seconds)
65512b0
  	{
65512b0
! 	    /* Display the file name every second or so, show the user we are
65512b0
! 	     * working on it. */
65512b0
  	    seconds = time(NULL);
65512b0
  	    msg_start();
65512b0
! 	    p = msg_strtrunc(fname, TRUE);
65512b0
  	    if (p == NULL)
65512b0
! 		msg_outtrans(fname);
65512b0
  	    else
65512b0
  	    {
65512b0
  		msg_outtrans(p);
65512b0
***************
65512b0
*** 3111,3117 ****
65512b0
  
65512b0
  	    /* Load file into a buffer, so that 'fileencoding' is detected,
65512b0
  	     * autocommands applied, etc. */
65512b0
! 	    buf = load_dummy_buffer(fnames[fi]);
65512b0
  
65512b0
  	    p_mls = save_mls;
65512b0
  #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
65512b0
--- 3121,3139 ----
65512b0
  
65512b0
  	    /* Load file into a buffer, so that 'fileencoding' is detected,
65512b0
  	     * autocommands applied, etc. */
65512b0
! 	    buf = load_dummy_buffer(fname);
65512b0
! 
65512b0
! 	    /* When autocommands changed directory: go back.  We assume it was
65512b0
! 	     * ":lcd %:p:h". */
65512b0
! 	    mch_dirname(dirname_now, MAXPATHL);
65512b0
! 	    if (STRCMP(dirname_start, dirname_now) != 0)
65512b0
! 	    {
65512b0
! 		exarg_T ea;
65512b0
! 
65512b0
! 		ea.arg = dirname_start;
65512b0
! 		ea.cmdidx = CMD_lcd;
65512b0
! 		ex_cd(&ea);
65512b0
! 	    }
65512b0
  
65512b0
  	    p_mls = save_mls;
65512b0
  #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
65512b0
***************
65512b0
*** 3125,3131 ****
65512b0
  	if (buf == NULL)
65512b0
  	{
65512b0
  	    if (!got_int)
65512b0
! 		smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
65512b0
  	}
65512b0
  	else
65512b0
  	{
65512b0
--- 3147,3153 ----
65512b0
  	if (buf == NULL)
65512b0
  	{
65512b0
  	    if (!got_int)
65512b0
! 		smsg((char_u *)_("Cannot open file \"%s\""), fname);
65512b0
  	}
65512b0
  	else
65512b0
  	{
65512b0
***************
65512b0
*** 3139,3147 ****
65512b0
  		while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
65512b0
  								     col) > 0)
65512b0
  		{
65512b0
  		    if (qf_add_entry(qi, &prevp,
65512b0
  				NULL,       /* dir */
65512b0
! 				fnames[fi],
65512b0
  				0,
65512b0
  				ml_get_buf(buf,
65512b0
  				     regmatch.startpos[0].lnum + lnum, FALSE),
65512b0
--- 3161,3170 ----
65512b0
  		while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
65512b0
  								     col) > 0)
65512b0
  		{
65512b0
+ 		    ;
65512b0
  		    if (qf_add_entry(qi, &prevp,
65512b0
  				NULL,       /* dir */
65512b0
! 				fname,
65512b0
  				0,
65512b0
  				ml_get_buf(buf,
65512b0
  				     regmatch.startpos[0].lnum + lnum, FALSE),
65512b0
***************
65512b0
*** 3209,3214 ****
65512b0
--- 3232,3244 ----
65512b0
  
65512b0
  		if (buf != NULL)
65512b0
  		{
65512b0
+ 		    /* If the buffer is still loaded we need to use the
65512b0
+ 		     * directory we jumped to below. */
65512b0
+ 		    if (buf == first_match_buf
65512b0
+ 			    && target_dir == NULL
65512b0
+ 			    && STRCMP(dirname_start, dirname_now) != 0)
65512b0
+ 			target_dir = vim_strsave(dirname_now);
65512b0
+ 
65512b0
  		    /* The buffer is still loaded, the Filetype autocommands
65512b0
  		     * need to be done now, in that buffer.  And the modelines
65512b0
  		     * need to be done (again).  But not the window-local
65512b0
***************
65512b0
*** 3252,3257 ****
65512b0
--- 3282,3297 ----
65512b0
  		/* If we jumped to another buffer redrawing will already be
65512b0
  		 * taken care of. */
65512b0
  		redraw_for_dummy = FALSE;
65512b0
+ 
65512b0
+ 	    /* Jump to the directory used after loading the buffer. */
65512b0
+ 	    if (curbuf == first_match_buf && target_dir != NULL)
65512b0
+ 	    {
65512b0
+ 		exarg_T ea;
65512b0
+ 
65512b0
+ 		ea.arg = target_dir;
65512b0
+ 		ea.cmdidx = CMD_lcd;
65512b0
+ 		ex_cd(&ea);
65512b0
+ 	    }
65512b0
  	}
65512b0
      }
65512b0
      else
65512b0
***************
65512b0
*** 3269,3274 ****
65512b0
--- 3309,3315 ----
65512b0
      }
65512b0
  
65512b0
  theend:
65512b0
+     vim_free(target_dir);
65512b0
      vim_free(regmatch.regprog);
65512b0
  }
65512b0
  
65512b0
*** ../vim-7.1.125/src/version.c	Sat Sep 29 14:15:00 2007
65512b0
--- src/version.c	Sun Sep 30 13:41:30 2007
65512b0
***************
65512b0
*** 668,669 ****
65512b0
--- 668,671 ----
65512b0
  {   /* Add new patch number below this line */
65512b0
+ /**/
65512b0
+     126,
65512b0
  /**/
65512b0
65512b0
-- 
65512b0
The MS-Windows registry is no more hostile than any other bunch of state
65512b0
information... that is held in a binary format... a format that nobody
65512b0
understands... and is replicated and cached in a complex and largely
65512b0
undocumented way... and contains large amounts of duplicate and obfuscated
65512b0
information...  (Ben Peterson)
65512b0
65512b0
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
65512b0
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
65512b0
\\\        download, build and distribute -- http://www.A-A-P.org        ///
65512b0
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///