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