5267cd2
To: vim-dev@vim.org
5267cd2
Subject: Patch 7.2.100
5267cd2
Fcc: outbox
5267cd2
From: Bram Moolenaar <Bram@moolenaar.net>
5267cd2
Mime-Version: 1.0
5267cd2
Content-Type: text/plain; charset=ISO-8859-1
5267cd2
Content-Transfer-Encoding: 8bit
5267cd2
------------
5267cd2
5267cd2
Patch 7.2.100
5267cd2
Problem:    When using ":source" on a FIFO or something else that can't rewind
5267cd2
	    the first three bytes are skipped.
5267cd2
Solution:   Instead of rewinding read the first line and detect a BOM in that.
5267cd2
	    (mostly by James Vega)
5267cd2
Files:	    src/ex_cmds2.c
5267cd2
5267cd2
5267cd2
*** ../vim-7.2.099/src/ex_cmds2.c	Sat Nov 15 14:10:23 2008
5267cd2
--- src/ex_cmds2.c	Wed Feb  4 16:05:51 2009
5267cd2
***************
5267cd2
*** 2842,2847 ****
5267cd2
--- 2842,2848 ----
5267cd2
      linenr_T		    save_sourcing_lnum;
5267cd2
      char_u		    *p;
5267cd2
      char_u		    *fname_exp;
5267cd2
+     char_u		    *firstline = NULL;
5267cd2
      int			    retval = FAIL;
5267cd2
  #ifdef FEAT_EVAL
5267cd2
      scid_T		    save_current_SID;
5267cd2
***************
5267cd2
*** 2992,3014 ****
5267cd2
  
5267cd2
      cookie.level = ex_nesting_level;
5267cd2
  #endif
5267cd2
- #ifdef FEAT_MBYTE
5267cd2
-     cookie.conv.vc_type = CONV_NONE;		/* no conversion */
5267cd2
- 
5267cd2
-     /* Try reading the first few bytes to check for a UTF-8 BOM. */
5267cd2
-     {
5267cd2
- 	char_u	    buf[3];
5267cd2
- 
5267cd2
- 	if (fread((char *)buf, sizeof(char_u), (size_t)3, cookie.fp)
5267cd2
- 								  == (size_t)3
5267cd2
- 		&& buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
5267cd2
- 	    /* Found BOM, setup conversion and skip over it. */
5267cd2
- 	    convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
5267cd2
- 	else
5267cd2
- 	    /* No BOM found, rewind. */
5267cd2
- 	    fseek(cookie.fp, 0L, SEEK_SET);
5267cd2
-     }
5267cd2
- #endif
5267cd2
  
5267cd2
      /*
5267cd2
       * Keep the sourcing name/lnum, for recursive calls.
5267cd2
--- 2993,2998 ----
5267cd2
***************
5267cd2
*** 3018,3023 ****
5267cd2
--- 3002,3026 ----
5267cd2
      save_sourcing_lnum = sourcing_lnum;
5267cd2
      sourcing_lnum = 0;
5267cd2
  
5267cd2
+ #ifdef FEAT_MBYTE
5267cd2
+     cookie.conv.vc_type = CONV_NONE;		/* no conversion */
5267cd2
+ 
5267cd2
+     /* Read the first line so we can check for a UTF-8 BOM. */
5267cd2
+     firstline = getsourceline(0, (void *)&cookie, 0);
5267cd2
+     if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
5267cd2
+ 			      && firstline[1] == 0xbb && firstline[2] == 0xbf)
5267cd2
+     {
5267cd2
+ 	/* Found BOM; setup conversion, skip over BOM and recode the line. */
5267cd2
+ 	convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
5267cd2
+ 	p = string_convert(&cookie.conv, firstline + 3, NULL);
5267cd2
+ 	if (p != NULL)
5267cd2
+ 	{
5267cd2
+ 	    vim_free(firstline);
5267cd2
+ 	    firstline = p;
5267cd2
+ 	}
5267cd2
+     }
5267cd2
+ #endif
5267cd2
+ 
5267cd2
  #ifdef STARTUPTIME
5267cd2
      time_push(&tv_rel, &tv_start);
5267cd2
  #endif
5267cd2
***************
5267cd2
*** 3111,3119 ****
5267cd2
      /*
5267cd2
       * Call do_cmdline, which will call getsourceline() to get the lines.
5267cd2
       */
5267cd2
!     do_cmdline(NULL, getsourceline, (void *)&cookie,
5267cd2
  				     DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
5267cd2
- 
5267cd2
      retval = OK;
5267cd2
  
5267cd2
  #ifdef FEAT_PROFILE
5267cd2
--- 3114,3121 ----
5267cd2
      /*
5267cd2
       * Call do_cmdline, which will call getsourceline() to get the lines.
5267cd2
       */
5267cd2
!     do_cmdline(firstline, getsourceline, (void *)&cookie,
5267cd2
  				     DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
5267cd2
      retval = OK;
5267cd2
  
5267cd2
  #ifdef FEAT_PROFILE
5267cd2
***************
5267cd2
*** 3171,3176 ****
5267cd2
--- 3173,3179 ----
5267cd2
  #endif
5267cd2
      fclose(cookie.fp);
5267cd2
      vim_free(cookie.nextline);
5267cd2
+     vim_free(firstline);
5267cd2
  #ifdef FEAT_MBYTE
5267cd2
      convert_setup(&cookie.conv, NULL, NULL);
5267cd2
  #endif
5267cd2
*** ../vim-7.2.099/src/version.c	Wed Feb  4 17:27:50 2009
5267cd2
--- src/version.c	Wed Feb  4 17:48:47 2009
5267cd2
***************
5267cd2
*** 678,679 ****
5267cd2
--- 678,681 ----
5267cd2
  {   /* Add new patch number below this line */
5267cd2
+ /**/
5267cd2
+     100,
5267cd2
  /**/
5267cd2
5267cd2
-- 
5267cd2
Well, you come from nothing, you go back to nothing...  What have you
5267cd2
lost?  Nothing!
5267cd2
				-- Monty Python: The life of Brian
5267cd2
5267cd2
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
5267cd2
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
5267cd2
\\\        download, build and distribute -- http://www.A-A-P.org        ///
5267cd2
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///