Karsten Hopp 1ac50e
To: vim_dev@googlegroups.com
Karsten Hopp 1ac50e
Subject: Patch 7.4.016
Karsten Hopp 1ac50e
Fcc: outbox
Karsten Hopp 1ac50e
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 1ac50e
Mime-Version: 1.0
Karsten Hopp 1ac50e
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 1ac50e
Content-Transfer-Encoding: 8bit
Karsten Hopp 1ac50e
------------
Karsten Hopp 1ac50e
Karsten Hopp 1ac50e
Patch 7.4.016
Karsten Hopp 1ac50e
Problem:    MS-Windows: File name completion doesn't work properly with
Karsten Hopp 1ac50e
	    Chinese characters. (Yue Wu)
Karsten Hopp 1ac50e
Solution:   Add fname_casew(). (Ken Takata)
Karsten Hopp 1ac50e
Files:	    src/os_win32.c
Karsten Hopp 1ac50e
Karsten Hopp 1ac50e
Karsten Hopp 1ac50e
*** ../vim-7.4.015/src/os_win32.c	2013-08-30 17:11:29.000000000 +0200
Karsten Hopp 1ac50e
--- src/os_win32.c	2013-08-30 17:28:30.000000000 +0200
Karsten Hopp 1ac50e
***************
Karsten Hopp 1ac50e
*** 2500,2508 ****
Karsten Hopp 1ac50e
--- 2500,2624 ----
Karsten Hopp 1ac50e
  }
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
+ #ifdef FEAT_MBYTE
Karsten Hopp 1ac50e
+ /*
Karsten Hopp 1ac50e
+  * fname_casew(): Wide version of fname_case().  Set the case of the file name,
Karsten Hopp 1ac50e
+  * if it already exists.  When "len" is > 0, also expand short to long
Karsten Hopp 1ac50e
+  * filenames.
Karsten Hopp 1ac50e
+  * Return FAIL if wide functions are not available, OK otherwise.
Karsten Hopp 1ac50e
+  * NOTE: much of this is identical to fname_case(), keep in sync!
Karsten Hopp 1ac50e
+  */
Karsten Hopp 1ac50e
+     static int
Karsten Hopp 1ac50e
+ fname_casew(
Karsten Hopp 1ac50e
+     WCHAR	*name,
Karsten Hopp 1ac50e
+     int		len)
Karsten Hopp 1ac50e
+ {
Karsten Hopp 1ac50e
+     WCHAR		szTrueName[_MAX_PATH + 2];
Karsten Hopp 1ac50e
+     WCHAR		szTrueNameTemp[_MAX_PATH + 2];
Karsten Hopp 1ac50e
+     WCHAR		*ptrue, *ptruePrev;
Karsten Hopp 1ac50e
+     WCHAR		*porig, *porigPrev;
Karsten Hopp 1ac50e
+     int			flen;
Karsten Hopp 1ac50e
+     WIN32_FIND_DATAW	fb;
Karsten Hopp 1ac50e
+     HANDLE		hFind;
Karsten Hopp 1ac50e
+     int			c;
Karsten Hopp 1ac50e
+     int			slen;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     flen = (int)wcslen(name);
Karsten Hopp 1ac50e
+     if (flen > _MAX_PATH)
Karsten Hopp 1ac50e
+ 	return OK;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     /* slash_adjust(name) not needed, already adjusted by fname_case(). */
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     /* Build the new name in szTrueName[] one component at a time. */
Karsten Hopp 1ac50e
+     porig = name;
Karsten Hopp 1ac50e
+     ptrue = szTrueName;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     if (iswalpha(porig[0]) && porig[1] == L':')
Karsten Hopp 1ac50e
+     {
Karsten Hopp 1ac50e
+ 	/* copy leading drive letter */
Karsten Hopp 1ac50e
+ 	*ptrue++ = *porig++;
Karsten Hopp 1ac50e
+ 	*ptrue++ = *porig++;
Karsten Hopp 1ac50e
+ 	*ptrue = NUL;	    /* in case nothing follows */
Karsten Hopp 1ac50e
+     }
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     while (*porig != NUL)
Karsten Hopp 1ac50e
+     {
Karsten Hopp 1ac50e
+ 	/* copy \ characters */
Karsten Hopp 1ac50e
+ 	while (*porig == psepc)
Karsten Hopp 1ac50e
+ 	    *ptrue++ = *porig++;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	ptruePrev = ptrue;
Karsten Hopp 1ac50e
+ 	porigPrev = porig;
Karsten Hopp 1ac50e
+ 	while (*porig != NUL && *porig != psepc)
Karsten Hopp 1ac50e
+ 	{
Karsten Hopp 1ac50e
+ 	    *ptrue++ = *porig++;
Karsten Hopp 1ac50e
+ 	}
Karsten Hopp 1ac50e
+ 	*ptrue = NUL;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	/* To avoid a slow failure append "\*" when searching a directory,
Karsten Hopp 1ac50e
+ 	 * server or network share. */
Karsten Hopp 1ac50e
+ 	wcscpy(szTrueNameTemp, szTrueName);
Karsten Hopp 1ac50e
+ 	slen = (int)wcslen(szTrueNameTemp);
Karsten Hopp 1ac50e
+ 	if (*porig == psepc && slen + 2 < _MAX_PATH)
Karsten Hopp 1ac50e
+ 	    wcscpy(szTrueNameTemp + slen, L"\\*");
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	/* Skip "", "." and "..". */
Karsten Hopp 1ac50e
+ 	if (ptrue > ptruePrev
Karsten Hopp 1ac50e
+ 		&& (ptruePrev[0] != L'.'
Karsten Hopp 1ac50e
+ 		    || (ptruePrev[1] != NUL
Karsten Hopp 1ac50e
+ 			&& (ptruePrev[1] != L'.' || ptruePrev[2] != NUL)))
Karsten Hopp 1ac50e
+ 		&& (hFind = FindFirstFileW(szTrueNameTemp, &fb))
Karsten Hopp 1ac50e
+ 						      != INVALID_HANDLE_VALUE)
Karsten Hopp 1ac50e
+ 	{
Karsten Hopp 1ac50e
+ 	    c = *porig;
Karsten Hopp 1ac50e
+ 	    *porig = NUL;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	    /* Only use the match when it's the same name (ignoring case) or
Karsten Hopp 1ac50e
+ 	     * expansion is allowed and there is a match with the short name
Karsten Hopp 1ac50e
+ 	     * and there is enough room. */
Karsten Hopp 1ac50e
+ 	    if (_wcsicoll(porigPrev, fb.cFileName) == 0
Karsten Hopp 1ac50e
+ 		    || (len > 0
Karsten Hopp 1ac50e
+ 			&& (_wcsicoll(porigPrev, fb.cAlternateFileName) == 0
Karsten Hopp 1ac50e
+ 			    && (int)(ptruePrev - szTrueName)
Karsten Hopp 1ac50e
+ 					   + (int)wcslen(fb.cFileName) < len)))
Karsten Hopp 1ac50e
+ 	    {
Karsten Hopp 1ac50e
+ 		wcscpy(ptruePrev, fb.cFileName);
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 		/* Look for exact match and prefer it if found.  Must be a
Karsten Hopp 1ac50e
+ 		 * long name, otherwise there would be only one match. */
Karsten Hopp 1ac50e
+ 		while (FindNextFileW(hFind, &fb))
Karsten Hopp 1ac50e
+ 		{
Karsten Hopp 1ac50e
+ 		    if (*fb.cAlternateFileName != NUL
Karsten Hopp 1ac50e
+ 			    && (wcscoll(porigPrev, fb.cFileName) == 0
Karsten Hopp 1ac50e
+ 				|| (len > 0
Karsten Hopp 1ac50e
+ 				    && (_wcsicoll(porigPrev,
Karsten Hopp 1ac50e
+ 						   fb.cAlternateFileName) == 0
Karsten Hopp 1ac50e
+ 				    && (int)(ptruePrev - szTrueName)
Karsten Hopp 1ac50e
+ 					 + (int)wcslen(fb.cFileName) < len))))
Karsten Hopp 1ac50e
+ 		    {
Karsten Hopp 1ac50e
+ 			wcscpy(ptruePrev, fb.cFileName);
Karsten Hopp 1ac50e
+ 			break;
Karsten Hopp 1ac50e
+ 		    }
Karsten Hopp 1ac50e
+ 		}
Karsten Hopp 1ac50e
+ 	    }
Karsten Hopp 1ac50e
+ 	    FindClose(hFind);
Karsten Hopp 1ac50e
+ 	    *porig = c;
Karsten Hopp 1ac50e
+ 	    ptrue = ptruePrev + wcslen(ptruePrev);
Karsten Hopp 1ac50e
+ 	}
Karsten Hopp 1ac50e
+ 	else if (hFind == INVALID_HANDLE_VALUE
Karsten Hopp 1ac50e
+ 		&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Karsten Hopp 1ac50e
+ 	    return FAIL;
Karsten Hopp 1ac50e
+     }
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     wcscpy(name, szTrueName);
Karsten Hopp 1ac50e
+     return OK;
Karsten Hopp 1ac50e
+ }
Karsten Hopp 1ac50e
+ #endif
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
  /*
Karsten Hopp 1ac50e
   * fname_case(): Set the case of the file name, if it already exists.
Karsten Hopp 1ac50e
   * When "len" is > 0, also expand short to long filenames.
Karsten Hopp 1ac50e
+  * NOTE: much of this is identical to fname_casew(), keep in sync!
Karsten Hopp 1ac50e
   */
Karsten Hopp 1ac50e
      void
Karsten Hopp 1ac50e
  fname_case(
Karsten Hopp 1ac50e
***************
Karsten Hopp 1ac50e
*** 2520,2530 ****
Karsten Hopp 1ac50e
      int			slen;
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
      flen = (int)STRLEN(name);
Karsten Hopp 1ac50e
!     if (flen == 0 || flen > _MAX_PATH)
Karsten Hopp 1ac50e
  	return;
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
      slash_adjust(name);
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
      /* Build the new name in szTrueName[] one component at a time. */
Karsten Hopp 1ac50e
      porig = name;
Karsten Hopp 1ac50e
      ptrue = szTrueName;
Karsten Hopp 1ac50e
--- 2636,2679 ----
Karsten Hopp 1ac50e
      int			slen;
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
      flen = (int)STRLEN(name);
Karsten Hopp 1ac50e
!     if (flen == 0)
Karsten Hopp 1ac50e
  	return;
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
      slash_adjust(name);
Karsten Hopp 1ac50e
  
Karsten Hopp 1ac50e
+ #ifdef FEAT_MBYTE
Karsten Hopp 1ac50e
+     if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Karsten Hopp 1ac50e
+     {
Karsten Hopp 1ac50e
+ 	WCHAR	*p = enc_to_utf16(name, NULL);
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	if (p != NULL)
Karsten Hopp 1ac50e
+ 	{
Karsten Hopp 1ac50e
+ 	    char_u	*q;
Karsten Hopp 1ac50e
+ 	    WCHAR	buf[_MAX_PATH + 2];
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	    wcscpy(buf, p);
Karsten Hopp 1ac50e
+ 	    vim_free(p);
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+ 	    if (fname_casew(buf, (len > 0) ? _MAX_PATH : 0) == OK)
Karsten Hopp 1ac50e
+ 	    {
Karsten Hopp 1ac50e
+ 		q = utf16_to_enc(buf, NULL);
Karsten Hopp 1ac50e
+ 		if (q != NULL)
Karsten Hopp 1ac50e
+ 		{
Karsten Hopp 1ac50e
+ 		    vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
Karsten Hopp 1ac50e
+ 		    vim_free(q);
Karsten Hopp 1ac50e
+ 		    return;
Karsten Hopp 1ac50e
+ 		}
Karsten Hopp 1ac50e
+ 	    }
Karsten Hopp 1ac50e
+ 	}
Karsten Hopp 1ac50e
+ 	/* Retry with non-wide function (for Windows 98). */
Karsten Hopp 1ac50e
+     }
Karsten Hopp 1ac50e
+ #endif
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
+     /* If 'enc' is utf-8, flen can be larger than _MAX_PATH.
Karsten Hopp 1ac50e
+      * So we should check this after calling wide function. */
Karsten Hopp 1ac50e
+     if (flen > _MAX_PATH)
Karsten Hopp 1ac50e
+ 	return;
Karsten Hopp 1ac50e
+ 
Karsten Hopp 1ac50e
      /* Build the new name in szTrueName[] one component at a time. */
Karsten Hopp 1ac50e
      porig = name;
Karsten Hopp 1ac50e
      ptrue = szTrueName;
Karsten Hopp 1ac50e
*** ../vim-7.4.015/src/version.c	2013-08-30 17:11:29.000000000 +0200
Karsten Hopp 1ac50e
--- src/version.c	2013-08-30 17:15:06.000000000 +0200
Karsten Hopp 1ac50e
***************
Karsten Hopp 1ac50e
*** 740,741 ****
Karsten Hopp 1ac50e
--- 740,743 ----
Karsten Hopp 1ac50e
  {   /* Add new patch number below this line */
Karsten Hopp 1ac50e
+ /**/
Karsten Hopp 1ac50e
+     16,
Karsten Hopp 1ac50e
  /**/
Karsten Hopp 1ac50e
Karsten Hopp 1ac50e
-- 
Karsten Hopp 1ac50e
Fingers not found - Pound head on keyboard to continue.
Karsten Hopp 1ac50e
Karsten Hopp 1ac50e
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 1ac50e
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 1ac50e
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp 1ac50e
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///