81c2858
To: vim-dev@vim.org
81c2858
Subject: Patch 7.2.245
81c2858
Fcc: outbox
81c2858
From: Bram Moolenaar <Bram@moolenaar.net>
81c2858
Mime-Version: 1.0
81c2858
Content-Type: text/plain; charset=UTF-8
81c2858
Content-Transfer-Encoding: 8bit
81c2858
------------
81c2858
81c2858
Patch 7.2.245
81c2858
Problem:    When 'enc' is "utf-16" and 'fenc' is "utf-8" writing a file does
81c2858
	    conversion while none should be done. (Yukihiro Nakadaira) When
81c2858
	    'fenc' is empty the file is written as utf-8 instead of utf-16.
81c2858
Solution:   Do proper comparison of encodings, taking into account that all
81c2858
	    Unicode values for 'enc' use utf-8 internally.
81c2858
Files:	    src/fileio.c
81c2858
81c2858
81c2858
*** ../vim-7.2.244/src/fileio.c	2009-07-29 18:05:57.000000000 +0200
81c2858
--- src/fileio.c	2009-07-29 17:04:06.000000000 +0200
81c2858
***************
81c2858
*** 134,140 ****
81c2858
  #ifdef FEAT_MBYTE
81c2858
  static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
81c2858
  static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
81c2858
! static int same_encoding __ARGS((char_u *a, char_u *b));
81c2858
  static int get_fio_flags __ARGS((char_u *ptr));
81c2858
  static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
81c2858
  static int make_bom __ARGS((char_u *buf, char_u *name));
81c2858
--- 134,140 ----
81c2858
  #ifdef FEAT_MBYTE
81c2858
  static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
81c2858
  static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
81c2858
! static int need_conversion __ARGS((char_u *fenc));
81c2858
  static int get_fio_flags __ARGS((char_u *ptr));
81c2858
  static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
81c2858
  static int make_bom __ARGS((char_u *buf, char_u *name));
81c2858
***************
81c2858
*** 1043,1055 ****
81c2858
      }
81c2858
  
81c2858
      /*
81c2858
!      * Conversion is required when the encoding of the file is different
81c2858
!      * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4 (requires
81c2858
!      * conversion to UTF-8).
81c2858
       */
81c2858
      fio_flags = 0;
81c2858
!     converted = (*fenc != NUL && !same_encoding(p_enc, fenc));
81c2858
!     if (converted || enc_unicode != 0)
81c2858
      {
81c2858
  
81c2858
  	/* "ucs-bom" means we need to check the first bytes of the file
81c2858
--- 1043,1054 ----
81c2858
      }
81c2858
  
81c2858
      /*
81c2858
!      * Conversion may be required when the encoding of the file is different
81c2858
!      * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
81c2858
       */
81c2858
      fio_flags = 0;
81c2858
!     converted = need_conversion(fenc);
81c2858
!     if (converted)
81c2858
      {
81c2858
  
81c2858
  	/* "ucs-bom" means we need to check the first bytes of the file
81c2858
***************
81c2858
*** 3969,3978 ****
81c2858
  	fenc = buf->b_p_fenc;
81c2858
  
81c2858
      /*
81c2858
!      * The file needs to be converted when 'fileencoding' is set and
81c2858
!      * 'fileencoding' differs from 'encoding'.
81c2858
       */
81c2858
!     converted = (*fenc != NUL && !same_encoding(p_enc, fenc));
81c2858
  
81c2858
      /*
81c2858
       * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done.  Or
81c2858
--- 3968,3976 ----
81c2858
  	fenc = buf->b_p_fenc;
81c2858
  
81c2858
      /*
81c2858
!      * Check if the file needs to be converted.
81c2858
       */
81c2858
!     converted = need_conversion(fenc);
81c2858
  
81c2858
      /*
81c2858
       * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done.  Or
81c2858
***************
81c2858
*** 5502,5521 ****
81c2858
  }
81c2858
  
81c2858
  /*
81c2858
!  * Return TRUE if "a" and "b" are the same 'encoding'.
81c2858
!  * Ignores difference between "ansi" and "latin1", "ucs-4" and "ucs-4be", etc.
81c2858
   */
81c2858
      static int
81c2858
! same_encoding(a, b)
81c2858
!     char_u	*a;
81c2858
!     char_u	*b;
81c2858
  {
81c2858
!     int		f;
81c2858
  
81c2858
!     if (STRCMP(a, b) == 0)
81c2858
! 	return TRUE;
81c2858
!     f = get_fio_flags(a);
81c2858
!     return (f != 0 && get_fio_flags(b) == f);
81c2858
  }
81c2858
  
81c2858
  /*
81c2858
--- 5500,5536 ----
81c2858
  }
81c2858
  
81c2858
  /*
81c2858
!  * Return TRUE if file encoding "fenc" requires conversion from or to
81c2858
!  * 'encoding'.
81c2858
   */
81c2858
      static int
81c2858
! need_conversion(fenc)
81c2858
!     char_u	*fenc;
81c2858
  {
81c2858
!     int		same_encoding;
81c2858
!     int		enc_flags;
81c2858
!     int		fenc_flags;
81c2858
  
81c2858
!     if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
81c2858
! 	same_encoding = TRUE;
81c2858
!     else
81c2858
!     {
81c2858
! 	/* Ignore difference between "ansi" and "latin1", "ucs-4" and
81c2858
! 	 * "ucs-4be", etc. */
81c2858
! 	enc_flags = get_fio_flags(p_enc);
81c2858
! 	fenc_flags = get_fio_flags(fenc);
81c2858
! 	same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
81c2858
!     }
81c2858
!     if (same_encoding)
81c2858
!     {
81c2858
! 	/* Specified encoding matches with 'encoding'.  This requires
81c2858
! 	 * conversion when 'encoding' is Unicode but not UTF-8. */
81c2858
! 	return enc_unicode != 0;
81c2858
!     }
81c2858
! 
81c2858
!     /* Encodings differ.  However, conversion is not needed when 'enc' is any
81c2858
!      * Unicode encoding and the file is UTF-8. */
81c2858
!     return !(enc_utf8 && fenc_flags == FIO_UTF8);
81c2858
  }
81c2858
  
81c2858
  /*
81c2858
*** ../vim-7.2.244/src/version.c	2009-07-29 18:05:57.000000000 +0200
81c2858
--- src/version.c	2009-07-29 18:20:08.000000000 +0200
81c2858
***************
81c2858
*** 678,679 ****
81c2858
--- 678,681 ----
81c2858
  {   /* Add new patch number below this line */
81c2858
+ /**/
81c2858
+     245,
81c2858
  /**/
81c2858
81c2858
-- 
81c2858
An actual excerpt from a classified section of a city newspaper:
81c2858
"Illiterate?  Write today for free help!"
81c2858
81c2858
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
81c2858
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
81c2858
\\\        download, build and distribute -- http://www.A-A-P.org        ///
81c2858
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///