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