Karsten Hopp e373ee
To: vim_dev@googlegroups.com
Karsten Hopp e373ee
Subject: Patch 7.3.836
Karsten Hopp e373ee
Fcc: outbox
Karsten Hopp e373ee
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp e373ee
Mime-Version: 1.0
Karsten Hopp e373ee
Content-Type: text/plain; charset=UTF-8
Karsten Hopp e373ee
Content-Transfer-Encoding: 8bit
Karsten Hopp e373ee
------------
Karsten Hopp e373ee
Karsten Hopp e373ee
Patch 7.3.836
Karsten Hopp e373ee
Problem:    Clipboard does not work on Win32 when compiled with Cygwin.
Karsten Hopp e373ee
Solution:   Move the Win32 clipboard code to a separate file and use it when
Karsten Hopp e373ee
	    building with os_unix.c. (Frodak Baksik, Ken Takata)
Karsten Hopp e373ee
Files:	    src/Make_bc5.mak, src/Make_cyg.mak, src/Make_ivc.mak,
Karsten Hopp e373ee
	    src/Make_ming.mak, src/Make_mvc.mak, src/Make_w16.mak,
Karsten Hopp e373ee
	    src/Makefile, src/config.h.in, src/configure.in,
Karsten Hopp e373ee
	    src/auto/configure, src/feature.h, src/globals.h, src/mbyte.c,
Karsten Hopp e373ee
	    src/os_mswin.c, src/os_unix.c, src/os_win32.c, src/proto.h,
Karsten Hopp e373ee
	    src/proto/os_mswin.pro, src/proto/winclip.pro, src/term.c,
Karsten Hopp e373ee
	    src/vim.h, src/winclip.c
Karsten Hopp e373ee
Karsten Hopp e373ee
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_bc5.mak	2010-08-15 21:57:32.000000000 +0200
Karsten Hopp e373ee
--- src/Make_bc5.mak	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 694,700 ****
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  !if ($(OSTYPE)==WIN32)
Karsten Hopp e373ee
  vimobj = $(vimobj) \
Karsten Hopp e373ee
! 	$(OBJDIR)\os_win32.obj $(OBJDIR)\os_mswin.obj
Karsten Hopp e373ee
  !elif ($(OSTYPE)==DOS16)
Karsten Hopp e373ee
  vimobj = $(vimobj) \
Karsten Hopp e373ee
  	$(OBJDIR)\os_msdos.obj
Karsten Hopp e373ee
--- 694,700 ----
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  !if ($(OSTYPE)==WIN32)
Karsten Hopp e373ee
  vimobj = $(vimobj) \
Karsten Hopp e373ee
! 	$(OBJDIR)\os_win32.obj $(OBJDIR)\os_mswin.obj $(OBJDIR)\winclip.obj
Karsten Hopp e373ee
  !elif ($(OSTYPE)==DOS16)
Karsten Hopp e373ee
  vimobj = $(vimobj) \
Karsten Hopp e373ee
  	$(OBJDIR)\os_msdos.obj
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_cyg.mak	2013-02-20 13:34:14.000000000 +0100
Karsten Hopp e373ee
--- src/Make_cyg.mak	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 547,552 ****
Karsten Hopp e373ee
--- 547,553 ----
Karsten Hopp e373ee
  	$(OUTDIR)/option.o \
Karsten Hopp e373ee
  	$(OUTDIR)/os_win32.o \
Karsten Hopp e373ee
  	$(OUTDIR)/os_mswin.o \
Karsten Hopp e373ee
+ 	$(OUTDIR)/winclip.o \
Karsten Hopp e373ee
  	$(OUTDIR)/pathdef.o \
Karsten Hopp e373ee
  	$(OUTDIR)/popupmnu.o \
Karsten Hopp e373ee
  	$(OUTDIR)/quickfix.o \
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_ivc.mak	2010-08-15 21:57:28.000000000 +0200
Karsten Hopp e373ee
--- src/Make_ivc.mak	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 241,246 ****
Karsten Hopp e373ee
--- 241,247 ----
Karsten Hopp e373ee
  	"$(INTDIR)/ops.obj" \
Karsten Hopp e373ee
  	"$(INTDIR)/option.obj" \
Karsten Hopp e373ee
  	"$(INTDIR)/os_mswin.obj" \
Karsten Hopp e373ee
+ 	"$(INTDIR)/winclip.obj" \
Karsten Hopp e373ee
  	"$(INTDIR)/os_win32.obj" \
Karsten Hopp e373ee
  	"$(INTDIR)/popupmnu.obj" \
Karsten Hopp e373ee
  	"$(INTDIR)/quickfix.obj" \
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 600,605 ****
Karsten Hopp e373ee
--- 601,610 ----
Karsten Hopp e373ee
  # End Source File
Karsten Hopp e373ee
  # Begin Source File
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ SOURCE=.\winclip.c
Karsten Hopp e373ee
+ # End Source File
Karsten Hopp e373ee
+ # Begin Source File
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
  SOURCE=.\os_win32.c
Karsten Hopp e373ee
  # End Source File
Karsten Hopp e373ee
  # Begin Source File
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_ming.mak	2013-01-30 14:55:35.000000000 +0100
Karsten Hopp e373ee
--- src/Make_ming.mak	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 523,528 ****
Karsten Hopp e373ee
--- 523,529 ----
Karsten Hopp e373ee
  	$(OUTDIR)/option.o \
Karsten Hopp e373ee
  	$(OUTDIR)/os_win32.o \
Karsten Hopp e373ee
  	$(OUTDIR)/os_mswin.o \
Karsten Hopp e373ee
+ 	$(OUTDIR)/winclip.o \
Karsten Hopp e373ee
  	$(OUTDIR)/pathdef.o \
Karsten Hopp e373ee
  	$(OUTDIR)/popupmnu.o \
Karsten Hopp e373ee
  	$(OUTDIR)/quickfix.o \
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_mvc.mak	2013-01-30 14:55:35.000000000 +0100
Karsten Hopp e373ee
--- src/Make_mvc.mak	2013-02-26 14:29:05.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 543,548 ****
Karsten Hopp e373ee
--- 543,549 ----
Karsten Hopp e373ee
  	$(OUTDIR)\ops.obj \
Karsten Hopp e373ee
  	$(OUTDIR)\option.obj \
Karsten Hopp e373ee
  	$(OUTDIR)\os_mswin.obj \
Karsten Hopp e373ee
+ 	$(OUTDIR)\winclip.obj \
Karsten Hopp e373ee
  	$(OUTDIR)\os_win32.obj \
Karsten Hopp e373ee
  	$(OUTDIR)\pathdef.obj \
Karsten Hopp e373ee
  	$(OUTDIR)\popupmnu.obj \
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1149,1154 ****
Karsten Hopp e373ee
--- 1150,1157 ----
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  $(OUTDIR)/os_mswin.obj:	$(OUTDIR) os_mswin.c  $(INCL)
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ $(OUTDIR)/winclip.obj:	$(OUTDIR) winclip.c  $(INCL)
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
  $(OUTDIR)/os_win32.obj:	$(OUTDIR) os_win32.c  $(INCL) os_win32.h
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  $(OUTDIR)/os_w32exe.obj:	$(OUTDIR) os_w32exe.c  $(INCL)
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1256,1261 ****
Karsten Hopp e373ee
--- 1259,1265 ----
Karsten Hopp e373ee
  	proto/ops.pro \
Karsten Hopp e373ee
  	proto/option.pro \
Karsten Hopp e373ee
  	proto/os_mswin.pro \
Karsten Hopp e373ee
+ 	proto/winclip.pro \
Karsten Hopp e373ee
  	proto/os_win32.pro \
Karsten Hopp e373ee
  	proto/popupmnu.pro \
Karsten Hopp e373ee
  	proto/quickfix.pro \
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Make_w16.mak	2010-08-15 21:57:27.000000000 +0200
Karsten Hopp e373ee
--- src/Make_w16.mak	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 107,112 ****
Karsten Hopp e373ee
--- 107,113 ----
Karsten Hopp e373ee
     $(INTDIR)\os_win16.obj\
Karsten Hopp e373ee
     $(INTDIR)\os_msdos.obj\
Karsten Hopp e373ee
     $(INTDIR)\os_mswin.obj\
Karsten Hopp e373ee
+    $(INTDIR)\winclip.obj\
Karsten Hopp e373ee
     $(INTDIR)\popupmnu.obj\
Karsten Hopp e373ee
     $(INTDIR)\quickfix.obj\
Karsten Hopp e373ee
     $(INTDIR)\regexp.obj\
Karsten Hopp e373ee
*** ../vim-7.3.835/src/Makefile	2013-01-30 17:41:43.000000000 +0100
Karsten Hopp e373ee
--- src/Makefile	2013-02-26 14:54:51.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1638,1644 ****
Karsten Hopp e373ee
  RSRC_DIR = os_mac_rsrc
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  PRO_MANUAL = os_amiga.pro os_msdos.pro os_win16.pro os_win32.pro \
Karsten Hopp e373ee
! 	os_mswin.pro os_beos.pro os_vms.pro $(PERL_PRO)
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  # Default target is making the executable and tools
Karsten Hopp e373ee
  all: $(VIMTARGET) $(TOOLS) languages $(GUI_BUNDLE)
Karsten Hopp e373ee
--- 1638,1644 ----
Karsten Hopp e373ee
  RSRC_DIR = os_mac_rsrc
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  PRO_MANUAL = os_amiga.pro os_msdos.pro os_win16.pro os_win32.pro \
Karsten Hopp e373ee
! 	os_mswin.pro winclip.pro os_beos.pro os_vms.pro $(PERL_PRO)
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  # Default target is making the executable and tools
Karsten Hopp e373ee
  all: $(VIMTARGET) $(TOOLS) languages $(GUI_BUNDLE)
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1792,1797 ****
Karsten Hopp e373ee
--- 1792,1801 ----
Karsten Hopp e373ee
  	$(CPROTO) -DWIN16 -DWIN32 -UHAVE_CONFIG_H $< > proto/$@
Karsten Hopp e373ee
  	echo "/* vim: set ft=c : */" >> proto/$@
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ winclip.pro: winclip.c
Karsten Hopp e373ee
+ 	$(CPROTO) -DWIN16 -DWIN32 -UHAVE_CONFIG_H $< > proto/$@
Karsten Hopp e373ee
+ 	echo "/* vim: set ft=c : */" >> proto/$@
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
  os_beos.pro: os_beos.c
Karsten Hopp e373ee
  	$(CPROTO) -D__BEOS__ -UHAVE_CONFIG_H $< > proto/$@
Karsten Hopp e373ee
  	echo "/* vim: set ft=c : */" >> proto/$@
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 2642,2647 ****
Karsten Hopp e373ee
--- 2646,2657 ----
Karsten Hopp e373ee
  objects/os_unix.o: os_unix.c
Karsten Hopp e373ee
  	$(CCC) -o $@ os_unix.c
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ objects/os_mswin.o: os_mswin.c
Karsten Hopp e373ee
+ 	$(CCC) -o $@ os_mswin.c
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ objects/winclip.o: winclip.c
Karsten Hopp e373ee
+ 	$(CCC) -o $@ winclip.c
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
  objects/pathdef.o: auto/pathdef.c
Karsten Hopp e373ee
  	$(CCC) -o $@ auto/pathdef.c
Karsten Hopp e373ee
  
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 2970,2975 ****
Karsten Hopp e373ee
--- 2980,2989 ----
Karsten Hopp e373ee
   ascii.h keymap.h term.h macros.h option.h structs.h regexp.h gui.h \
Karsten Hopp e373ee
   gui_beval.h proto/gui_beval.pro ex_cmds.h proto.h globals.h farsi.h \
Karsten Hopp e373ee
   arabic.h version.h
Karsten Hopp e373ee
+ objects/winclip.o: winclip.c vimio.h vim.h auto/config.h feature.h os_unix.h \
Karsten Hopp e373ee
+  auto/osdef.h ascii.h keymap.h term.h macros.h option.h structs.h \
Karsten Hopp e373ee
+  regexp.h gui.h ex_cmds.h proto.h globals.h farsi.h arabic.h \
Karsten Hopp e373ee
+  proto/winclip.pro
Karsten Hopp e373ee
  objects/window.o: window.c vim.h auto/config.h feature.h os_unix.h auto/osdef.h \
Karsten Hopp e373ee
   ascii.h keymap.h term.h macros.h option.h structs.h regexp.h gui.h \
Karsten Hopp e373ee
   gui_beval.h proto/gui_beval.pro ex_cmds.h proto.h globals.h farsi.h \
Karsten Hopp e373ee
*** ../vim-7.3.835/src/config.h.in	2012-08-15 14:04:50.000000000 +0200
Karsten Hopp e373ee
--- src/config.h.in	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 439,441 ****
Karsten Hopp e373ee
--- 439,444 ----
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  /* Define if fcntl()'s F_SETFD command knows about FD_CLOEXEC */
Karsten Hopp e373ee
  #undef HAVE_FD_CLOEXEC
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /* Define if you want Cygwin to use the WIN32 clipboard, not compatible with X11*/
Karsten Hopp e373ee
+ #undef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
*** ../vim-7.3.835/src/configure.in	2013-02-06 19:49:37.000000000 +0100
Karsten Hopp e373ee
--- src/configure.in	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 2474,2479 ****
Karsten Hopp e373ee
--- 2474,2494 ----
Karsten Hopp e373ee
  dnl end of GUI-checking
Karsten Hopp e373ee
  dnl ---------------------------------------------------------------------------
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ dnl Check for Cygwin, which needs an extra source file if not using X11
Karsten Hopp e373ee
+ AC_MSG_CHECKING(for CYGWIN environment)
Karsten Hopp e373ee
+ case `uname` in
Karsten Hopp e373ee
+     CYGWIN*)    CYGWIN=yes; AC_MSG_RESULT(yes)
Karsten Hopp e373ee
+                 AC_MSG_CHECKING(for CYGWIN clipboard support)
Karsten Hopp e373ee
+                 if test "x$with_x" = "xno" ; then
Karsten Hopp e373ee
+                   OS_EXTRA_SRC=winclip.c; OS_EXTRA_OBJ=objects/winclip.o
Karsten Hopp e373ee
+                   AC_MSG_RESULT(yes)
Karsten Hopp e373ee
+                   AC_DEFINE(FEAT_CYGWIN_WIN32_CLIPBOARD)
Karsten Hopp e373ee
+                 else
Karsten Hopp e373ee
+                   AC_MSG_RESULT(no - using X11)
Karsten Hopp e373ee
+                 fi ;;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     *)          CYGWIN=no; AC_MSG_RESULT(no);;
Karsten Hopp e373ee
+ esac
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  dnl Only really enable hangul input when GUI and XFONTSET are available
Karsten Hopp e373ee
  if test "$enable_hangulinput" = "yes"; then
Karsten Hopp e373ee
*** ../vim-7.3.835/src/auto/configure	2013-02-06 19:49:38.000000000 +0100
Karsten Hopp e373ee
--- src/auto/configure	2013-02-26 14:22:16.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 8846,8851 ****
Karsten Hopp e373ee
--- 8846,8872 ----
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  
Karsten Hopp e373ee
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for CYGWIN environment" >&5
Karsten Hopp e373ee
+ $as_echo_n "checking for CYGWIN environment... " >&6; }
Karsten Hopp e373ee
+ case `uname` in
Karsten Hopp e373ee
+     CYGWIN*)    CYGWIN=yes; { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
Karsten Hopp e373ee
+ $as_echo "yes" >&6; }
Karsten Hopp e373ee
+                 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for CYGWIN clipboard support" >&5
Karsten Hopp e373ee
+ $as_echo_n "checking for CYGWIN clipboard support... " >&6; }
Karsten Hopp e373ee
+                 if test "x$with_x" = "xno" ; then
Karsten Hopp e373ee
+                   OS_EXTRA_SRC=winclip.c; OS_EXTRA_OBJ=objects/winclip.o
Karsten Hopp e373ee
+                   { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
Karsten Hopp e373ee
+ $as_echo "yes" >&6; }
Karsten Hopp e373ee
+                   $as_echo "#define FEAT_CYGWIN_WIN32_CLIPBOARD 1" >>confdefs.h
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+                 else
Karsten Hopp e373ee
+                   { $as_echo "$as_me:${as_lineno-$LINENO}: result: no - using X11" >&5
Karsten Hopp e373ee
+ $as_echo "no - using X11" >&6; }
Karsten Hopp e373ee
+                 fi ;;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     *)          CYGWIN=no; { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
Karsten Hopp e373ee
+ $as_echo "no" >&6; };;
Karsten Hopp e373ee
+ esac
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  if test "$enable_hangulinput" = "yes"; then
Karsten Hopp e373ee
    if test "x$GUITYPE" = "xNONE"; then
Karsten Hopp e373ee
*** ../vim-7.3.835/src/feature.h	2013-02-14 22:11:31.000000000 +0100
Karsten Hopp e373ee
--- src/feature.h	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1129,1134 ****
Karsten Hopp e373ee
--- 1129,1139 ----
Karsten Hopp e373ee
   * +xterm_clipboard	Unix only: Include code for handling the clipboard
Karsten Hopp e373ee
   *			in an xterm like in the GUI.
Karsten Hopp e373ee
   */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
+ # define FEAT_CLIPBOARD
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
  #ifdef FEAT_GUI
Karsten Hopp e373ee
  # ifndef FEAT_CLIPBOARD
Karsten Hopp e373ee
  #  define FEAT_CLIPBOARD
Karsten Hopp e373ee
*** ../vim-7.3.835/src/globals.h	2012-08-23 18:43:06.000000000 +0200
Karsten Hopp e373ee
--- src/globals.h	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 802,808 ****
Karsten Hopp e373ee
  EXTERN int	enc_unicode INIT(= 0);	/* 2: UCS-2 or UTF-16, 4: UCS-4 */
Karsten Hopp e373ee
  EXTERN int	enc_utf8 INIT(= FALSE);		/* UTF-8 encoded Unicode */
Karsten Hopp e373ee
  EXTERN int	enc_latin1like INIT(= TRUE);	/* 'encoding' is latin1 comp. */
Karsten Hopp e373ee
! # ifdef WIN3264
Karsten Hopp e373ee
  /* Codepage nr of 'encoding'.  Negative means it's not been set yet, zero
Karsten Hopp e373ee
   * means 'encoding' is not a valid codepage. */
Karsten Hopp e373ee
  EXTERN int	enc_codepage INIT(= -1);
Karsten Hopp e373ee
--- 802,808 ----
Karsten Hopp e373ee
  EXTERN int	enc_unicode INIT(= 0);	/* 2: UCS-2 or UTF-16, 4: UCS-4 */
Karsten Hopp e373ee
  EXTERN int	enc_utf8 INIT(= FALSE);		/* UTF-8 encoded Unicode */
Karsten Hopp e373ee
  EXTERN int	enc_latin1like INIT(= TRUE);	/* 'encoding' is latin1 comp. */
Karsten Hopp e373ee
! # if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Karsten Hopp e373ee
  /* Codepage nr of 'encoding'.  Negative means it's not been set yet, zero
Karsten Hopp e373ee
   * means 'encoding' is not a valid codepage. */
Karsten Hopp e373ee
  EXTERN int	enc_codepage INIT(= -1);
Karsten Hopp e373ee
*** ../vim-7.3.835/src/mbyte.c	2013-02-13 16:49:54.000000000 +0100
Karsten Hopp e373ee
--- src/mbyte.c	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 613,619 ****
Karsten Hopp e373ee
      enc_dbcs = enc_dbcs_new;
Karsten Hopp e373ee
      has_mbyte = (enc_dbcs != 0 || enc_utf8);
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! #ifdef WIN3264
Karsten Hopp e373ee
      enc_codepage = encname2codepage(p_enc);
Karsten Hopp e373ee
      enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
--- 613,619 ----
Karsten Hopp e373ee
      enc_dbcs = enc_dbcs_new;
Karsten Hopp e373ee
      has_mbyte = (enc_dbcs != 0 || enc_utf8);
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! #if defined(WIN3264) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Karsten Hopp e373ee
      enc_codepage = encname2codepage(p_enc);
Karsten Hopp e373ee
      enc_latin9 = (STRCMP(p_enc, "iso-8859-15") == 0);
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 4089,4095 ****
Karsten Hopp e373ee
      return enc_canonize((char_u *)buf);
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! #if defined(WIN3264) || defined(PROTO)
Karsten Hopp e373ee
  /*
Karsten Hopp e373ee
   * Convert an encoding name to an MS-Windows codepage.
Karsten Hopp e373ee
   * Returns zero if no codepage can be figured out.
Karsten Hopp e373ee
--- 4089,4095 ----
Karsten Hopp e373ee
      return enc_canonize((char_u *)buf);
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! #if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Karsten Hopp e373ee
  /*
Karsten Hopp e373ee
   * Convert an encoding name to an MS-Windows codepage.
Karsten Hopp e373ee
   * Returns zero if no codepage can be figured out.
Karsten Hopp e373ee
*** ../vim-7.3.835/src/os_mswin.c	2012-11-20 16:53:34.000000000 +0100
Karsten Hopp e373ee
--- src/os_mswin.c	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 905,1640 ****
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
- #if defined(FEAT_MBYTE) || defined(PROTO)
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Convert an UTF-8 string to UTF-16.
Karsten Hopp e373ee
-  * "instr[inlen]" is the input.  "inlen" is in bytes.
Karsten Hopp e373ee
-  * When "outstr" is NULL only return the number of UTF-16 words produced.
Karsten Hopp e373ee
-  * Otherwise "outstr" must be a buffer of sufficient size.
Karsten Hopp e373ee
-  * Returns the number of UTF-16 words produced.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     int
Karsten Hopp e373ee
- utf8_to_utf16(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     int		outlen = 0;
Karsten Hopp e373ee
-     char_u	*p = instr;
Karsten Hopp e373ee
-     int		todo = inlen;
Karsten Hopp e373ee
-     int		l;
Karsten Hopp e373ee
-     int		ch;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     while (todo > 0)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	/* Only convert if we have a complete sequence. */
Karsten Hopp e373ee
- 	l = utf_ptr2len_len(p, todo);
Karsten Hopp e373ee
- 	if (l > todo)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    /* Return length of incomplete sequence. */
Karsten Hopp e373ee
- 	    if (unconvlenp != NULL)
Karsten Hopp e373ee
- 		*unconvlenp = todo;
Karsten Hopp e373ee
- 	    break;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	ch = utf_ptr2char(p);
Karsten Hopp e373ee
- 	if (ch >= 0x10000)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    /* non-BMP character, encoding with surrogate pairs */
Karsten Hopp e373ee
- 	    ++outlen;
Karsten Hopp e373ee
- 	    if (outstr != NULL)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		*outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10);
Karsten Hopp e373ee
- 		*outstr++ = 0xDC00 | (ch & 0x3FF);
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	else if (outstr != NULL)
Karsten Hopp e373ee
- 	    *outstr++ = ch;
Karsten Hopp e373ee
- 	++outlen;
Karsten Hopp e373ee
- 	p += l;
Karsten Hopp e373ee
- 	todo -= l;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     return outlen;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Convert an UTF-16 string to UTF-8.
Karsten Hopp e373ee
-  * The input is "instr[inlen]" with "inlen" in number of UTF-16 words.
Karsten Hopp e373ee
-  * When "outstr" is NULL only return the required number of bytes.
Karsten Hopp e373ee
-  * Otherwise "outstr" must be a buffer of sufficient size.
Karsten Hopp e373ee
-  * Return the number of bytes produced.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     int
Karsten Hopp e373ee
- utf16_to_utf8(short_u *instr, int inlen, char_u *outstr)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     int		outlen = 0;
Karsten Hopp e373ee
-     int		todo = inlen;
Karsten Hopp e373ee
-     short_u	*p = instr;
Karsten Hopp e373ee
-     int		l;
Karsten Hopp e373ee
-     int		ch, ch2;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     while (todo > 0)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	ch = *p;
Karsten Hopp e373ee
- 	if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    /* surrogate pairs handling */
Karsten Hopp e373ee
- 	    ch2 = p[1];
Karsten Hopp e373ee
- 	    if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000;
Karsten Hopp e373ee
- 		++p;
Karsten Hopp e373ee
- 		--todo;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	if (outstr != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    l = utf_char2bytes(ch, outstr);
Karsten Hopp e373ee
- 	    outstr += l;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	else
Karsten Hopp e373ee
- 	    l = utf_char2len(ch);
Karsten Hopp e373ee
- 	++p;
Karsten Hopp e373ee
- 	outlen += l;
Karsten Hopp e373ee
- 	--todo;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     return outlen;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Call MultiByteToWideChar() and allocate memory for the result.
Karsten Hopp e373ee
-  * Returns the result in "*out[*outlen]" with an extra zero appended.
Karsten Hopp e373ee
-  * "outlen" is in words.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- MultiByteToWideChar_alloc(UINT cp, DWORD flags,
Karsten Hopp e373ee
- 	LPCSTR in, int inlen,
Karsten Hopp e373ee
- 	LPWSTR *out, int *outlen)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
Karsten Hopp e373ee
-     /* Add one one word to avoid a zero-length alloc(). */
Karsten Hopp e373ee
-     *out = (LPWSTR)alloc(sizeof(WCHAR) * (*outlen + 1));
Karsten Hopp e373ee
-     if (*out != NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
Karsten Hopp e373ee
- 	(*out)[*outlen] = 0;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Call WideCharToMultiByte() and allocate memory for the result.
Karsten Hopp e373ee
-  * Returns the result in "*out[*outlen]" with an extra NUL appended.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- WideCharToMultiByte_alloc(UINT cp, DWORD flags,
Karsten Hopp e373ee
- 	LPCWSTR in, int inlen,
Karsten Hopp e373ee
- 	LPSTR *out, int *outlen,
Karsten Hopp e373ee
- 	LPCSTR def, LPBOOL useddef)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
Karsten Hopp e373ee
-     /* Add one one byte to avoid a zero-length alloc(). */
Karsten Hopp e373ee
-     *out = alloc((unsigned)*outlen + 1);
Karsten Hopp e373ee
-     if (*out != NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
Karsten Hopp e373ee
- 	(*out)[*outlen] = 0;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #endif /* FEAT_MBYTE */
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Clipboard stuff, for cutting and pasting text to other windows.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /* Type used for the clipboard type of Vim's data. */
Karsten Hopp e373ee
- typedef struct
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     int type;		/* MCHAR, MBLOCK or MLINE */
Karsten Hopp e373ee
-     int txtlen;		/* length of CF_TEXT in bytes */
Karsten Hopp e373ee
-     int ucslen;		/* length of CF_UNICODETEXT in words */
Karsten Hopp e373ee
-     int rawlen;		/* length of clip_star.format_raw, including encoding,
Karsten Hopp e373ee
- 			   excluding terminating NUL */
Karsten Hopp e373ee
- } VimClipType_t;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Make vim the owner of the current selection.  Return OK upon success.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
- /*ARGSUSED*/
Karsten Hopp e373ee
-     int
Karsten Hopp e373ee
- clip_mch_own_selection(VimClipboard *cbd)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     /*
Karsten Hopp e373ee
-      * Never actually own the clipboard.  If another application sets the
Karsten Hopp e373ee
-      * clipboard, we don't want to think that we still own it.
Karsten Hopp e373ee
-      */
Karsten Hopp e373ee
-     return FAIL;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Make vim NOT the owner of the current selection.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
- /*ARGSUSED*/
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- clip_mch_lose_selection(VimClipboard *cbd)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     /* Nothing needs to be done here */
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Copy "str[*size]" into allocated memory, changing CR-NL to NL.
Karsten Hopp e373ee
-  * Return the allocated result and the size in "*size".
Karsten Hopp e373ee
-  * Returns NULL when out of memory.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     static char_u *
Karsten Hopp e373ee
- crnl_to_nl(const char_u *str, int *size)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     int		pos = 0;
Karsten Hopp e373ee
-     int		str_len = *size;
Karsten Hopp e373ee
-     char_u	*ret;
Karsten Hopp e373ee
-     char_u	*retp;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Avoid allocating zero bytes, it generates an error message. */
Karsten Hopp e373ee
-     ret = lalloc((long_u)(str_len == 0 ? 1 : str_len), TRUE);
Karsten Hopp e373ee
-     if (ret != NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	retp = ret;
Karsten Hopp e373ee
- 	for (pos = 0; pos < str_len; ++pos)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    if (str[pos] == '\r' && str[pos + 1] == '\n')
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		++pos;
Karsten Hopp e373ee
- 		--(*size);
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    *retp++ = str[pos];
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     return ret;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Wait for another process to Close the Clipboard.
Karsten Hopp e373ee
-  * Returns TRUE for success.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     static int
Karsten Hopp e373ee
- vim_open_clipboard(void)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     int delay = 10;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     while (!OpenClipboard(NULL))
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	if (delay > 500)
Karsten Hopp e373ee
- 	    return FALSE;  /* waited too long, give up */
Karsten Hopp e373ee
- 	Sleep(delay);
Karsten Hopp e373ee
- 	delay *= 2;	/* wait for 10, 20, 40, 80, etc. msec */
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
-     return TRUE;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Get the current selection and put it in the clipboard register.
Karsten Hopp e373ee
-  *
Karsten Hopp e373ee
-  * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
Karsten Hopp e373ee
-  * On NT/W95 the clipboard data is a fixed global memory object and
Karsten Hopp e373ee
-  * so its handle = its pointer.
Karsten Hopp e373ee
-  * On Win32s, however, co-operation with the Win16 system means that
Karsten Hopp e373ee
-  * the clipboard data is moveable and its handle is not a pointer at all,
Karsten Hopp e373ee
-  * so we can't just cast the return value of GetClipboardData to (char_u*).
Karsten Hopp e373ee
-  * <VN>
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- clip_mch_request_selection(VimClipboard *cbd)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     VimClipType_t	metadata = { -1, -1, -1, -1 };
Karsten Hopp e373ee
-     HGLOBAL		hMem = NULL;
Karsten Hopp e373ee
-     char_u		*str = NULL;
Karsten Hopp e373ee
- #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     char_u		*to_free = NULL;
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- #ifdef FEAT_MBYTE
Karsten Hopp e373ee
-     HGLOBAL		rawh = NULL;
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
-     int			str_size = 0;
Karsten Hopp e373ee
-     int			maxlen;
Karsten Hopp e373ee
-     size_t		n;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /*
Karsten Hopp e373ee
-      * Don't pass GetActiveWindow() as an argument to OpenClipboard() because
Karsten Hopp e373ee
-      * then we can't paste back into the same window for some reason - webb.
Karsten Hopp e373ee
-      */
Karsten Hopp e373ee
-     if (!vim_open_clipboard())
Karsten Hopp e373ee
- 	return;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Check for vim's own clipboard format first.  This only gets the type of
Karsten Hopp e373ee
-      * the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
Karsten Hopp e373ee
-     if (IsClipboardFormatAvailable(cbd->format))
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	VimClipType_t	*meta_p;
Karsten Hopp e373ee
- 	HGLOBAL		meta_h;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	/* We have metadata on the clipboard; try to get it. */
Karsten Hopp e373ee
- 	if ((meta_h = GetClipboardData(cbd->format)) != NULL
Karsten Hopp e373ee
- 		&& (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    /* The size of "VimClipType_t" changed, "rawlen" was added later.
Karsten Hopp e373ee
- 	     * Only copy what is available for backwards compatibility. */
Karsten Hopp e373ee
- 	    n = sizeof(VimClipType_t);
Karsten Hopp e373ee
- 	    if (GlobalSize(meta_h) < n)
Karsten Hopp e373ee
- 		n = GlobalSize(meta_h);
Karsten Hopp e373ee
- 	    memcpy(&metadata, meta_p, n);
Karsten Hopp e373ee
- 	    GlobalUnlock(meta_h);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #ifdef FEAT_MBYTE
Karsten Hopp e373ee
-     /* Check for Vim's raw clipboard format first.  This is used without
Karsten Hopp e373ee
-      * conversion, but only if 'encoding' matches. */
Karsten Hopp e373ee
-     if (IsClipboardFormatAvailable(cbd->format_raw)
Karsten Hopp e373ee
- 				      && metadata.rawlen > (int)STRLEN(p_enc))
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	/* We have raw data on the clipboard; try to get it. */
Karsten Hopp e373ee
- 	if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    char_u	*rawp;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	    rawp = (char_u *)GlobalLock(rawh);
Karsten Hopp e373ee
- 	    if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		n = STRLEN(p_enc) + 1;
Karsten Hopp e373ee
- 		str = rawp + n;
Karsten Hopp e373ee
- 		str_size = (int)(metadata.rawlen - n);
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    else
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		GlobalUnlock(rawh);
Karsten Hopp e373ee
- 		rawh = NULL;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
-     if (str == NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     /* Try to get the clipboard in Unicode if it's not an empty string. */
Karsten Hopp e373ee
-     if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	HGLOBAL hMemW;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	    /* Use the length of our metadata if possible, but limit it to the
Karsten Hopp e373ee
- 	     * GlobalSize() for safety. */
Karsten Hopp e373ee
- 	    maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR));
Karsten Hopp e373ee
- 	    if (metadata.ucslen >= 0)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		if (metadata.ucslen > maxlen)
Karsten Hopp e373ee
- 		    str_size = maxlen;
Karsten Hopp e373ee
- 		else
Karsten Hopp e373ee
- 		    str_size = metadata.ucslen;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    else
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		for (str_size = 0; str_size < maxlen; ++str_size)
Karsten Hopp e373ee
- 		    if (hMemWstr[str_size] == NUL)
Karsten Hopp e373ee
- 			break;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size);
Karsten Hopp e373ee
- 	    GlobalUnlock(hMemW);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
-     else
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
-     /* Get the clipboard in the Active codepage. */
Karsten Hopp e373ee
-     if (IsClipboardFormatAvailable(CF_TEXT))
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    str = (char_u *)GlobalLock(hMem);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	    /* The length is either what our metadata says or the strlen().
Karsten Hopp e373ee
- 	     * But limit it to the GlobalSize() for safety. */
Karsten Hopp e373ee
- 	    maxlen = (int)GlobalSize(hMem);
Karsten Hopp e373ee
- 	    if (metadata.txtlen >= 0)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		if (metadata.txtlen > maxlen)
Karsten Hopp e373ee
- 		    str_size = maxlen;
Karsten Hopp e373ee
- 		else
Karsten Hopp e373ee
- 		    str_size = metadata.txtlen;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    else
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		for (str_size = 0; str_size < maxlen; ++str_size)
Karsten Hopp e373ee
- 		    if (str[str_size] == NUL)
Karsten Hopp e373ee
- 			break;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
- 	    /* The text is in the active codepage.  Convert to 'encoding',
Karsten Hopp e373ee
- 	     * going through UTF-16. */
Karsten Hopp e373ee
- 	    acp_to_enc(str, str_size, &to_free, &maxlen);
Karsten Hopp e373ee
- 	    if (to_free != NULL)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		str_size = maxlen;
Karsten Hopp e373ee
- 		str = to_free;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- # endif
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- #ifdef FEAT_MBYTE
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     if (str != NULL && *str != NUL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	char_u *temp_clipboard;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	/* If the type is not known detect it. */
Karsten Hopp e373ee
- 	if (metadata.type == -1)
Karsten Hopp e373ee
- 	    metadata.type = MAUTO;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	/* Translate <CR><NL> into <NL>. */
Karsten Hopp e373ee
- 	temp_clipboard = crnl_to_nl(str, &str_size);
Karsten Hopp e373ee
- 	if (temp_clipboard != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
Karsten Hopp e373ee
- 	    vim_free(temp_clipboard);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* unlock the global object */
Karsten Hopp e373ee
-     if (hMem != NULL)
Karsten Hopp e373ee
- 	GlobalUnlock(hMem);
Karsten Hopp e373ee
- #ifdef FEAT_MBYTE
Karsten Hopp e373ee
-     if (rawh != NULL)
Karsten Hopp e373ee
- 	GlobalUnlock(rawh);
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
-     CloseClipboard();
Karsten Hopp e373ee
- #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     vim_free(to_free);
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Send the current selection to the clipboard.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- clip_mch_set_selection(VimClipboard *cbd)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     char_u		*str = NULL;
Karsten Hopp e373ee
-     VimClipType_t	metadata;
Karsten Hopp e373ee
-     long_u		txtlen;
Karsten Hopp e373ee
-     HGLOBAL		hMemRaw = NULL;
Karsten Hopp e373ee
-     HGLOBAL		hMem = NULL;
Karsten Hopp e373ee
-     HGLOBAL		hMemVim = NULL;
Karsten Hopp e373ee
- # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     HGLOBAL		hMemW = NULL;
Karsten Hopp e373ee
- # endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* If the '*' register isn't already filled in, fill it in now */
Karsten Hopp e373ee
-     cbd->owned = TRUE;
Karsten Hopp e373ee
-     clip_get_selection(cbd);
Karsten Hopp e373ee
-     cbd->owned = FALSE;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Get the text to be put on the clipboard, with CR-LF. */
Karsten Hopp e373ee
-     metadata.type = clip_convert_selection(&str, &txtlen, cbd);
Karsten Hopp e373ee
-     if (metadata.type < 0)
Karsten Hopp e373ee
- 	return;
Karsten Hopp e373ee
-     metadata.txtlen = (int)txtlen;
Karsten Hopp e373ee
-     metadata.ucslen = 0;
Karsten Hopp e373ee
-     metadata.rawlen = 0;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #ifdef FEAT_MBYTE
Karsten Hopp e373ee
-     /* Always set the raw bytes: 'encoding', NUL and the text.  This is used
Karsten Hopp e373ee
-      * when copy/paste from/to Vim with the same 'encoding', so that illegal
Karsten Hopp e373ee
-      * bytes can also be copied and no conversion is needed. */
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	LPSTR lpszMemRaw;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1);
Karsten Hopp e373ee
- 	hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
Karsten Hopp e373ee
- 							 metadata.rawlen + 1);
Karsten Hopp e373ee
- 	lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
Karsten Hopp e373ee
- 	if (lpszMemRaw != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    STRCPY(lpszMemRaw, p_enc);
Karsten Hopp e373ee
- 	    memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
Karsten Hopp e373ee
- 	    GlobalUnlock(hMemRaw);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	else
Karsten Hopp e373ee
- 	    metadata.rawlen = 0;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	WCHAR		*out;
Karsten Hopp e373ee
- 	int		len = metadata.txtlen;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	/* Convert the text to UTF-16. This is put on the clipboard as
Karsten Hopp e373ee
- 	 * CF_UNICODETEXT. */
Karsten Hopp e373ee
- 	out = (WCHAR *)enc_to_utf16(str, &len;;
Karsten Hopp e373ee
- 	if (out != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    WCHAR *lpszMemW;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	    /* Convert the text for CF_TEXT to Active codepage. Otherwise it's
Karsten Hopp e373ee
- 	     * p_enc, which has no relation to the Active codepage. */
Karsten Hopp e373ee
- 	    metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
Karsten Hopp e373ee
- 							       NULL, 0, 0, 0);
Karsten Hopp e373ee
- 	    vim_free(str);
Karsten Hopp e373ee
- 	    str = (char_u *)alloc((unsigned)(metadata.txtlen == 0 ? 1
Karsten Hopp e373ee
- 							  : metadata.txtlen));
Karsten Hopp e373ee
- 	    if (str == NULL)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		vim_free(out);
Karsten Hopp e373ee
- 		return;		/* out of memory */
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    WideCharToMultiByte(GetACP(), 0, out, len,
Karsten Hopp e373ee
- 						  str, metadata.txtlen, 0, 0);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	    /* Allocate memory for the UTF-16 text, add one NUL word to
Karsten Hopp e373ee
- 	     * terminate the string. */
Karsten Hopp e373ee
- 	    hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
Karsten Hopp e373ee
- 						   (len + 1) * sizeof(WCHAR));
Karsten Hopp e373ee
- 	    lpszMemW = (WCHAR *)GlobalLock(hMemW);
Karsten Hopp e373ee
- 	    if (lpszMemW != NULL)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		memcpy(lpszMemW, out, len * sizeof(WCHAR));
Karsten Hopp e373ee
- 		lpszMemW[len] = NUL;
Karsten Hopp e373ee
- 		GlobalUnlock(hMemW);
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- 	    vim_free(out);
Karsten Hopp e373ee
- 	    metadata.ucslen = len;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- # endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Allocate memory for the text, add one NUL byte to terminate the string.
Karsten Hopp e373ee
-      */
Karsten Hopp e373ee
-     hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	if (lpszMem)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    vim_strncpy(lpszMem, str, metadata.txtlen);
Karsten Hopp e373ee
- 	    GlobalUnlock(hMem);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Set up metadata: */
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	VimClipType_t *lpszMemVim = NULL;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
Karsten Hopp e373ee
- 						       sizeof(VimClipType_t));
Karsten Hopp e373ee
- 	lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
Karsten Hopp e373ee
- 	memcpy(lpszMemVim, &metadata, sizeof(metadata));
Karsten Hopp e373ee
- 	GlobalUnlock(hMemVim);
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /*
Karsten Hopp e373ee
-      * Open the clipboard, clear it and put our text on it.
Karsten Hopp e373ee
-      * Always set our Vim format.  Put Unicode and plain text on it.
Karsten Hopp e373ee
-      *
Karsten Hopp e373ee
-      * Don't pass GetActiveWindow() as an argument to OpenClipboard()
Karsten Hopp e373ee
-      * because then we can't paste back into the same window for some
Karsten Hopp e373ee
-      * reason - webb.
Karsten Hopp e373ee
-      */
Karsten Hopp e373ee
-     if (vim_open_clipboard())
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	if (EmptyClipboard())
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    SetClipboardData(cbd->format, hMemVim);
Karsten Hopp e373ee
- 	    hMemVim = 0;
Karsten Hopp e373ee
- # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
- 	    if (hMemW != NULL)
Karsten Hopp e373ee
- 	    {
Karsten Hopp e373ee
- 		if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
Karsten Hopp e373ee
- 		    hMemW = NULL;
Karsten Hopp e373ee
- 	    }
Karsten Hopp e373ee
- # endif
Karsten Hopp e373ee
- 	    /* Always use CF_TEXT.  On Win98 Notepad won't obtain the
Karsten Hopp e373ee
- 	     * CF_UNICODETEXT text, only CF_TEXT. */
Karsten Hopp e373ee
- 	    SetClipboardData(CF_TEXT, hMem);
Karsten Hopp e373ee
- 	    hMem = 0;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	CloseClipboard();
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     vim_free(str);
Karsten Hopp e373ee
-     /* Free any allocations we didn't give to the clipboard: */
Karsten Hopp e373ee
-     if (hMemRaw)
Karsten Hopp e373ee
- 	GlobalFree(hMemRaw);
Karsten Hopp e373ee
-     if (hMem)
Karsten Hopp e373ee
- 	GlobalFree(hMem);
Karsten Hopp e373ee
- # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
-     if (hMemW)
Karsten Hopp e373ee
- 	GlobalFree(hMemW);
Karsten Hopp e373ee
- # endif
Karsten Hopp e373ee
-     if (hMemVim)
Karsten Hopp e373ee
- 	GlobalFree(hMemVim);
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #endif /* FEAT_CLIPBOARD */
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #if defined(FEAT_MBYTE) || defined(PROTO)
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Note: the following two functions are only guaranteed to work when using
Karsten Hopp e373ee
-  * valid MS-Windows codepages or when iconv() is available.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Convert "str" from 'encoding' to UTF-16.
Karsten Hopp e373ee
-  * Input in "str" with length "*lenp".  When "lenp" is NULL, use strlen().
Karsten Hopp e373ee
-  * Output is returned as an allocated string.  "*lenp" is set to the length of
Karsten Hopp e373ee
-  * the result.  A trailing NUL is always added.
Karsten Hopp e373ee
-  * Returns NULL when out of memory.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     short_u *
Karsten Hopp e373ee
- enc_to_utf16(char_u *str, int *lenp)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     vimconv_T	conv;
Karsten Hopp e373ee
-     WCHAR	*ret;
Karsten Hopp e373ee
-     char_u	*allocbuf = NULL;
Karsten Hopp e373ee
-     int		len_loc;
Karsten Hopp e373ee
-     int		length;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     if (lenp == NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	len_loc = (int)STRLEN(str) + 1;
Karsten Hopp e373ee
- 	lenp = &len_loc;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     if (enc_codepage > 0)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	/* We can do any CP### -> UTF-16 in one pass, and we can do it
Karsten Hopp e373ee
- 	 * without iconv() (convert_* may need iconv). */
Karsten Hopp e373ee
- 	MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
-     else
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	/* Use "latin1" by default, we might be called before we have p_enc
Karsten Hopp e373ee
- 	 * set up.  Convert to utf-8 first, works better with iconv().  Does
Karsten Hopp e373ee
- 	 * nothing if 'encoding' is "utf-8". */
Karsten Hopp e373ee
- 	conv.vc_type = CONV_NONE;
Karsten Hopp e373ee
- 	if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
Karsten Hopp e373ee
- 						   (char_u *)"utf-8") == FAIL)
Karsten Hopp e373ee
- 	    return NULL;
Karsten Hopp e373ee
- 	if (conv.vc_type != CONV_NONE)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    str = allocbuf = string_convert(&conv, str, lenp);
Karsten Hopp e373ee
- 	    if (str == NULL)
Karsten Hopp e373ee
- 		return NULL;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	convert_setup(&conv, NULL, NULL);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	length = utf8_to_utf16(str, *lenp, NULL, NULL);
Karsten Hopp e373ee
- 	ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
Karsten Hopp e373ee
- 	if (ret != NULL)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
Karsten Hopp e373ee
- 	    ret[length] = 0;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	vim_free(allocbuf);
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     *lenp = length;
Karsten Hopp e373ee
-     return (short_u *)ret;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Convert an UTF-16 string to 'encoding'.
Karsten Hopp e373ee
-  * Input in "str" with length (counted in wide characters) "*lenp".  When
Karsten Hopp e373ee
-  * "lenp" is NULL, use wcslen().
Karsten Hopp e373ee
-  * Output is returned as an allocated string.  If "*lenp" is not NULL it is
Karsten Hopp e373ee
-  * set to the length of the result.
Karsten Hopp e373ee
-  * Returns NULL when out of memory.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     char_u *
Karsten Hopp e373ee
- utf16_to_enc(short_u *str, int *lenp)
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     vimconv_T	conv;
Karsten Hopp e373ee
-     char_u	*utf8_str = NULL, *enc_str = NULL;
Karsten Hopp e373ee
-     int		len_loc;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     if (lenp == NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	len_loc = (int)wcslen(str) + 1;
Karsten Hopp e373ee
- 	lenp = &len_loc;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     if (enc_codepage > 0)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	/* We can do any UTF-16 -> CP### in one pass. */
Karsten Hopp e373ee
- 	int length;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
Karsten Hopp e373ee
- 					    (LPSTR *)&enc_str, &length, 0, 0);
Karsten Hopp e373ee
- 	*lenp = length;
Karsten Hopp e373ee
- 	return enc_str;
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     /* Avoid allocating zero bytes, it generates an error message. */
Karsten Hopp e373ee
-     utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
Karsten Hopp e373ee
-     if (utf8_str != NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	*lenp = utf16_to_utf8(str, *lenp, utf8_str);
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	/* We might be called before we have p_enc set up. */
Karsten Hopp e373ee
- 	conv.vc_type = CONV_NONE;
Karsten Hopp e373ee
- 	convert_setup(&conv, (char_u *)"utf-8",
Karsten Hopp e373ee
- 					    p_enc? p_enc: (char_u *)"latin1");
Karsten Hopp e373ee
- 	if (conv.vc_type == CONV_NONE)
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    /* p_enc is utf-8, so we're done. */
Karsten Hopp e373ee
- 	    enc_str = utf8_str;
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 	else
Karsten Hopp e373ee
- 	{
Karsten Hopp e373ee
- 	    enc_str = string_convert(&conv, utf8_str, lenp);
Karsten Hopp e373ee
- 	    vim_free(utf8_str);
Karsten Hopp e373ee
- 	}
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 	convert_setup(&conv, NULL, NULL);
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     return enc_str;
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- #endif /* FEAT_MBYTE */
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- #if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
Karsten Hopp e373ee
- /*
Karsten Hopp e373ee
-  * Convert from the active codepage to 'encoding'.
Karsten Hopp e373ee
-  * Input is "str[str_size]".
Karsten Hopp e373ee
-  * The result is in allocated memory: "out[outlen]".  With terminating NUL.
Karsten Hopp e373ee
-  */
Karsten Hopp e373ee
-     void
Karsten Hopp e373ee
- acp_to_enc(str, str_size, out, outlen)
Karsten Hopp e373ee
-     char_u	*str;
Karsten Hopp e373ee
-     int		str_size;
Karsten Hopp e373ee
-     char_u	**out;
Karsten Hopp e373ee
-     int		*outlen;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- {
Karsten Hopp e373ee
-     LPWSTR	widestr;
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
-     MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
Karsten Hopp e373ee
-     if (widestr != NULL)
Karsten Hopp e373ee
-     {
Karsten Hopp e373ee
- 	++*outlen;	/* Include the 0 after the string */
Karsten Hopp e373ee
- 	*out = utf16_to_enc((short_u *)widestr, outlen);
Karsten Hopp e373ee
- 	vim_free(widestr);
Karsten Hopp e373ee
-     }
Karsten Hopp e373ee
- }
Karsten Hopp e373ee
- #endif
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
- 
Karsten Hopp e373ee
  /*
Karsten Hopp e373ee
   * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
Karsten Hopp e373ee
   */
Karsten Hopp e373ee
--- 905,910 ----
Karsten Hopp e373ee
*** ../vim-7.3.835/src/os_unix.c	2013-01-30 14:55:35.000000000 +0100
Karsten Hopp e373ee
--- src/os_unix.c	2013-02-26 14:31:32.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 61,66 ****
Karsten Hopp e373ee
--- 61,71 ----
Karsten Hopp e373ee
  #  include <cygwin/version.h>
Karsten Hopp e373ee
  #  include <sys/cygwin.h>	/* for cygwin_conv_to_posix_path() and/or
Karsten Hopp e373ee
  				 * for cygwin_conv_path() */
Karsten Hopp e373ee
+ #  ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
+ #   define WIN32_LEAN_AND_MEAN
Karsten Hopp e373ee
+ #   include <windows.h>
Karsten Hopp e373ee
+ #   include "winclip.pro"
Karsten Hopp e373ee
+ #  endif
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1223,1228 ****
Karsten Hopp e373ee
--- 1228,1236 ----
Karsten Hopp e373ee
  #ifdef MACOS_CONVERT
Karsten Hopp e373ee
      mac_conv_init();
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
+ #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
+     win_clip_init();
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  
Karsten Hopp e373ee
      static void
Karsten Hopp e373ee
*** ../vim-7.3.835/src/os_win32.c	2013-02-13 16:49:54.000000000 +0100
Karsten Hopp e373ee
--- src/os_win32.c	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1851,1866 ****
Karsten Hopp e373ee
  	set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
!     clip_init(TRUE);
Karsten Hopp e373ee
! 
Karsten Hopp e373ee
!     /*
Karsten Hopp e373ee
!      * Vim's own clipboard format recognises whether the text is char, line,
Karsten Hopp e373ee
!      * or rectangular block.  Only useful for copying between two Vims.
Karsten Hopp e373ee
!      * "VimClipboard" was used for previous versions, using the first
Karsten Hopp e373ee
!      * character to specify MCHAR, MLINE or MBLOCK.
Karsten Hopp e373ee
!      */
Karsten Hopp e373ee
!     clip_star.format = RegisterClipboardFormat("VimClipboard2");
Karsten Hopp e373ee
!     clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  
Karsten Hopp e373ee
--- 1851,1857 ----
Karsten Hopp e373ee
  	set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
!     win_clip_init();
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  }
Karsten Hopp e373ee
  
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 2345,2360 ****
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
!     clip_init(TRUE);
Karsten Hopp e373ee
! 
Karsten Hopp e373ee
!     /*
Karsten Hopp e373ee
!      * Vim's own clipboard format recognises whether the text is char, line, or
Karsten Hopp e373ee
!      * rectangular block.  Only useful for copying between two Vims.
Karsten Hopp e373ee
!      * "VimClipboard" was used for previous versions, using the first
Karsten Hopp e373ee
!      * character to specify MCHAR, MLINE or MBLOCK.
Karsten Hopp e373ee
!      */
Karsten Hopp e373ee
!     clip_star.format = RegisterClipboardFormat("VimClipboard2");
Karsten Hopp e373ee
!     clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
      /* This will be NULL on anything but NT 4.0 */
Karsten Hopp e373ee
--- 2336,2342 ----
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
  #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
!     win_clip_init();
Karsten Hopp e373ee
  #endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
      /* This will be NULL on anything but NT 4.0 */
Karsten Hopp e373ee
*** ../vim-7.3.835/src/proto.h	2011-05-10 16:41:13.000000000 +0200
Karsten Hopp e373ee
--- src/proto.h	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 47,56 ****
Karsten Hopp e373ee
--- 47,58 ----
Karsten Hopp e373ee
     typedef int LPBOOL;
Karsten Hopp e373ee
  #  include "os_win16.pro"
Karsten Hopp e373ee
  #  include "os_mswin.pro"
Karsten Hopp e373ee
+ #  include "winclip.pro"
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
  # ifdef WIN3264
Karsten Hopp e373ee
  #  include "os_win32.pro"
Karsten Hopp e373ee
  #  include "os_mswin.pro"
Karsten Hopp e373ee
+ #  include "winclip.pro"
Karsten Hopp e373ee
  #  if (defined(__GNUC__) && !defined(__MINGW32__)) \
Karsten Hopp e373ee
  	|| (defined(__BORLANDC__) && __BORLANDC__ < 0x502)
Karsten Hopp e373ee
  extern int _stricoll __ARGS((char *a, char *b));
Karsten Hopp e373ee
*** ../vim-7.3.835/src/proto/os_mswin.pro	2012-11-20 16:56:49.000000000 +0100
Karsten Hopp e373ee
--- src/proto/os_mswin.pro	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 22,38 ****
Karsten Hopp e373ee
  int can_end_termcap_mode __ARGS((int give_msg));
Karsten Hopp e373ee
  int mch_screenmode __ARGS((char_u *arg));
Karsten Hopp e373ee
  int mch_libcall __ARGS((char_u *libname, char_u *funcname, char_u *argstring, int argint, char_u **string_result, int *number_result));
Karsten Hopp e373ee
- int utf8_to_utf16 __ARGS((char_u *instr, int inlen, short_u *outstr, int *unconvlenp));
Karsten Hopp e373ee
- int utf16_to_utf8 __ARGS((short_u *instr, int inlen, char_u *outstr));
Karsten Hopp e373ee
- void MultiByteToWideChar_alloc __ARGS((UINT cp, DWORD flags, LPCSTR in, int inlen, LPWSTR *out, int *outlen));
Karsten Hopp e373ee
- void WideCharToMultiByte_alloc __ARGS((UINT cp, DWORD flags, LPCWSTR in, int inlen, LPSTR *out, int *outlen, LPCSTR def, LPBOOL useddef));
Karsten Hopp e373ee
- int clip_mch_own_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
- void clip_mch_lose_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
- void clip_mch_request_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
- void clip_mch_set_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
- short_u *enc_to_utf16 __ARGS((char_u *str, int *lenp));
Karsten Hopp e373ee
- char_u *utf16_to_enc __ARGS((short_u *str, int *lenp));
Karsten Hopp e373ee
- void acp_to_enc __ARGS((char_u *str, int str_size, char_u **out, int *outlen));
Karsten Hopp e373ee
  void DumpPutS __ARGS((const char *psz));
Karsten Hopp e373ee
  int mch_get_winpos __ARGS((int *x, int *y));
Karsten Hopp e373ee
  void mch_set_winpos __ARGS((int x, int y));
Karsten Hopp e373ee
--- 22,27 ----
Karsten Hopp e373ee
*** ../vim-7.3.835/src/proto/winclip.pro	2013-02-26 14:55:20.000000000 +0100
Karsten Hopp e373ee
--- src/proto/winclip.pro	2013-02-26 14:32:52.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 0 ****
Karsten Hopp e373ee
--- 1,14 ----
Karsten Hopp e373ee
+ /* winclip.c */
Karsten Hopp e373ee
+ int utf8_to_utf16 __ARGS((char_u *instr, int inlen, short_u *outstr, int *unconvlenp));
Karsten Hopp e373ee
+ int utf16_to_utf8 __ARGS((short_u *instr, int inlen, char_u *outstr));
Karsten Hopp e373ee
+ void MultiByteToWideChar_alloc __ARGS((UINT cp, DWORD flags, LPCSTR in, int inlen, LPWSTR *out, int *outlen));
Karsten Hopp e373ee
+ void WideCharToMultiByte_alloc __ARGS((UINT cp, DWORD flags, LPCWSTR in, int inlen, LPSTR *out, int *outlen, LPCSTR def, LPBOOL useddef));
Karsten Hopp e373ee
+ void win_clip_init __ARGS((void));
Karsten Hopp e373ee
+ int clip_mch_own_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
+ void clip_mch_lose_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
+ void clip_mch_request_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
+ void clip_mch_set_selection __ARGS((VimClipboard *cbd));
Karsten Hopp e373ee
+ short_u *enc_to_utf16 __ARGS((char_u *str, int *lenp));
Karsten Hopp e373ee
+ char_u *utf16_to_enc __ARGS((short_u *str, int *lenp));
Karsten Hopp e373ee
+ void acp_to_enc __ARGS((char_u *str, int str_size, char_u **out, int *outlen));
Karsten Hopp e373ee
+ /* vim: set ft=c : */
Karsten Hopp e373ee
*** ../vim-7.3.835/src/term.c	2013-02-13 15:53:15.000000000 +0100
Karsten Hopp e373ee
--- src/term.c	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1853,1859 ****
Karsten Hopp e373ee
--- 1853,1861 ----
Karsten Hopp e373ee
  #    ifdef FEAT_GUI
Karsten Hopp e373ee
  	if (!gui.in_use)
Karsten Hopp e373ee
  #    endif
Karsten Hopp e373ee
+ #    ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
  	    clip_init(FALSE);
Karsten Hopp e373ee
+ #    endif
Karsten Hopp e373ee
  #   endif
Karsten Hopp e373ee
  	if (use_xterm_like_mouse(term))
Karsten Hopp e373ee
  	{
Karsten Hopp e373ee
*** ../vim-7.3.835/src/vim.h	2013-01-17 17:17:21.000000000 +0100
Karsten Hopp e373ee
--- src/vim.h	2013-02-26 14:18:19.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 1927,1933 ****
Karsten Hopp e373ee
      GdkAtom     gtk_sel_atom;	/* PRIMARY/CLIPBOARD selection ID */
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! # ifdef MSWIN
Karsten Hopp e373ee
      int_u	format;		/* Vim's own special clipboard format */
Karsten Hopp e373ee
      int_u	format_raw;	/* Vim's raw text clipboard format */
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
--- 1927,1933 ----
Karsten Hopp e373ee
      GdkAtom     gtk_sel_atom;	/* PRIMARY/CLIPBOARD selection ID */
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
  
Karsten Hopp e373ee
! # if defined(MSWIN) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
Karsten Hopp e373ee
      int_u	format;		/* Vim's own special clipboard format */
Karsten Hopp e373ee
      int_u	format_raw;	/* Vim's raw text clipboard format */
Karsten Hopp e373ee
  # endif
Karsten Hopp e373ee
*** ../vim-7.3.835/src/winclip.c	2013-02-26 14:55:20.000000000 +0100
Karsten Hopp e373ee
--- src/winclip.c	2013-02-26 14:32:42.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 0 ****
Karsten Hopp e373ee
--- 1,798 ----
Karsten Hopp e373ee
+ /* vi:set ts=8 sts=4 sw=4:
Karsten Hopp e373ee
+  *
Karsten Hopp e373ee
+  * VIM - Vi IMproved by Bram Moolenaar
Karsten Hopp e373ee
+  *
Karsten Hopp e373ee
+  * Do ":help uganda"  in Vim to read copying and usage conditions.
Karsten Hopp e373ee
+  * Do ":help credits" in Vim to see a list of people who contributed.
Karsten Hopp e373ee
+  * See README.txt for an overview of the Vim source code.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * winclip.c
Karsten Hopp e373ee
+  *
Karsten Hopp e373ee
+  * Routines common to both Win16 and Win32 for clipboard handling.
Karsten Hopp e373ee
+  * Also used by Cygwin, using os_unix.c.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #ifdef WIN16
Karsten Hopp e373ee
+ # ifdef __BORLANDC__
Karsten Hopp e373ee
+ #  pragma warn -par
Karsten Hopp e373ee
+ #  pragma warn -ucp
Karsten Hopp e373ee
+ #  pragma warn -use
Karsten Hopp e373ee
+ #  pragma warn -aus
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #include "vimio.h"
Karsten Hopp e373ee
+ #include "vim.h"
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Compile only the clipboard handling features when compiling for cygwin
Karsten Hopp e373ee
+  * posix environment.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
Karsten Hopp e373ee
+ # define WIN3264
Karsten Hopp e373ee
+ # define WIN32_LEAN_AND_MEAN
Karsten Hopp e373ee
+ # include <windows.h>
Karsten Hopp e373ee
+ # include "winclip.pro"
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * When generating prototypes for Win32 on Unix, these lines make the syntax
Karsten Hopp e373ee
+  * errors disappear.  They do not need to be correct.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ #ifdef PROTO
Karsten Hopp e373ee
+ #define WINAPI
Karsten Hopp e373ee
+ #define WINBASEAPI
Karsten Hopp e373ee
+ typedef int DWORD;
Karsten Hopp e373ee
+ typedef int LPBOOL;
Karsten Hopp e373ee
+ typedef int LPCSTR;
Karsten Hopp e373ee
+ typedef int LPCWSTR;
Karsten Hopp e373ee
+ typedef int LPSTR;
Karsten Hopp e373ee
+ typedef int LPWSTR;
Karsten Hopp e373ee
+ typedef int UINT;
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #if defined(FEAT_MBYTE) || defined(PROTO)
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Convert an UTF-8 string to UTF-16.
Karsten Hopp e373ee
+  * "instr[inlen]" is the input.  "inlen" is in bytes.
Karsten Hopp e373ee
+  * When "outstr" is NULL only return the number of UTF-16 words produced.
Karsten Hopp e373ee
+  * Otherwise "outstr" must be a buffer of sufficient size.
Karsten Hopp e373ee
+  * Returns the number of UTF-16 words produced.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     int
Karsten Hopp e373ee
+ utf8_to_utf16(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     int		outlen = 0;
Karsten Hopp e373ee
+     char_u	*p = instr;
Karsten Hopp e373ee
+     int		todo = inlen;
Karsten Hopp e373ee
+     int		l;
Karsten Hopp e373ee
+     int		ch;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     while (todo > 0)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	/* Only convert if we have a complete sequence. */
Karsten Hopp e373ee
+ 	l = utf_ptr2len_len(p, todo);
Karsten Hopp e373ee
+ 	if (l > todo)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    /* Return length of incomplete sequence. */
Karsten Hopp e373ee
+ 	    if (unconvlenp != NULL)
Karsten Hopp e373ee
+ 		*unconvlenp = todo;
Karsten Hopp e373ee
+ 	    break;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	ch = utf_ptr2char(p);
Karsten Hopp e373ee
+ 	if (ch >= 0x10000)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    /* non-BMP character, encoding with surrogate pairs */
Karsten Hopp e373ee
+ 	    ++outlen;
Karsten Hopp e373ee
+ 	    if (outstr != NULL)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		*outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10);
Karsten Hopp e373ee
+ 		*outstr++ = 0xDC00 | (ch & 0x3FF);
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	else if (outstr != NULL)
Karsten Hopp e373ee
+ 	    *outstr++ = ch;
Karsten Hopp e373ee
+ 	++outlen;
Karsten Hopp e373ee
+ 	p += l;
Karsten Hopp e373ee
+ 	todo -= l;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     return outlen;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Convert an UTF-16 string to UTF-8.
Karsten Hopp e373ee
+  * The input is "instr[inlen]" with "inlen" in number of UTF-16 words.
Karsten Hopp e373ee
+  * When "outstr" is NULL only return the required number of bytes.
Karsten Hopp e373ee
+  * Otherwise "outstr" must be a buffer of sufficient size.
Karsten Hopp e373ee
+  * Return the number of bytes produced.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     int
Karsten Hopp e373ee
+ utf16_to_utf8(short_u *instr, int inlen, char_u *outstr)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     int		outlen = 0;
Karsten Hopp e373ee
+     int		todo = inlen;
Karsten Hopp e373ee
+     short_u	*p = instr;
Karsten Hopp e373ee
+     int		l;
Karsten Hopp e373ee
+     int		ch, ch2;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     while (todo > 0)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	ch = *p;
Karsten Hopp e373ee
+ 	if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    /* surrogate pairs handling */
Karsten Hopp e373ee
+ 	    ch2 = p[1];
Karsten Hopp e373ee
+ 	    if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000;
Karsten Hopp e373ee
+ 		++p;
Karsten Hopp e373ee
+ 		--todo;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	if (outstr != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    l = utf_char2bytes(ch, outstr);
Karsten Hopp e373ee
+ 	    outstr += l;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	else
Karsten Hopp e373ee
+ 	    l = utf_char2len(ch);
Karsten Hopp e373ee
+ 	++p;
Karsten Hopp e373ee
+ 	outlen += l;
Karsten Hopp e373ee
+ 	--todo;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     return outlen;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Call MultiByteToWideChar() and allocate memory for the result.
Karsten Hopp e373ee
+  * Returns the result in "*out[*outlen]" with an extra zero appended.
Karsten Hopp e373ee
+  * "outlen" is in words.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ MultiByteToWideChar_alloc(UINT cp, DWORD flags,
Karsten Hopp e373ee
+ 	LPCSTR in, int inlen,
Karsten Hopp e373ee
+ 	LPWSTR *out, int *outlen)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
Karsten Hopp e373ee
+     /* Add one one word to avoid a zero-length alloc(). */
Karsten Hopp e373ee
+     *out = (LPWSTR)alloc(sizeof(WCHAR) * (*outlen + 1));
Karsten Hopp e373ee
+     if (*out != NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
Karsten Hopp e373ee
+ 	(*out)[*outlen] = 0;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Call WideCharToMultiByte() and allocate memory for the result.
Karsten Hopp e373ee
+  * Returns the result in "*out[*outlen]" with an extra NUL appended.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ WideCharToMultiByte_alloc(UINT cp, DWORD flags,
Karsten Hopp e373ee
+ 	LPCWSTR in, int inlen,
Karsten Hopp e373ee
+ 	LPSTR *out, int *outlen,
Karsten Hopp e373ee
+ 	LPCSTR def, LPBOOL useddef)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
Karsten Hopp e373ee
+     /* Add one one byte to avoid a zero-length alloc(). */
Karsten Hopp e373ee
+     *out = alloc((unsigned)*outlen + 1);
Karsten Hopp e373ee
+     if (*out != NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
Karsten Hopp e373ee
+ 	(*out)[*outlen] = 0;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #endif /* FEAT_MBYTE */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #ifdef FEAT_CLIPBOARD
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Clipboard stuff, for cutting and pasting text to other windows.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ win_clip_init(void)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     clip_init(TRUE);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /*
Karsten Hopp e373ee
+      * Vim's own clipboard format recognises whether the text is char, line,
Karsten Hopp e373ee
+      * or rectangular block.  Only useful for copying between two Vims.
Karsten Hopp e373ee
+      * "VimClipboard" was used for previous versions, using the first
Karsten Hopp e373ee
+      * character to specify MCHAR, MLINE or MBLOCK.
Karsten Hopp e373ee
+      */
Karsten Hopp e373ee
+     clip_star.format = RegisterClipboardFormat("VimClipboard2");
Karsten Hopp e373ee
+     clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /* Type used for the clipboard type of Vim's data. */
Karsten Hopp e373ee
+ typedef struct
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     int type;		/* MCHAR, MBLOCK or MLINE */
Karsten Hopp e373ee
+     int txtlen;		/* length of CF_TEXT in bytes */
Karsten Hopp e373ee
+     int ucslen;		/* length of CF_UNICODETEXT in words */
Karsten Hopp e373ee
+     int rawlen;		/* length of clip_star.format_raw, including encoding,
Karsten Hopp e373ee
+ 			   excluding terminating NUL */
Karsten Hopp e373ee
+ } VimClipType_t;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Make vim the owner of the current selection.  Return OK upon success.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ /*ARGSUSED*/
Karsten Hopp e373ee
+     int
Karsten Hopp e373ee
+ clip_mch_own_selection(VimClipboard *cbd)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     /*
Karsten Hopp e373ee
+      * Never actually own the clipboard.  If another application sets the
Karsten Hopp e373ee
+      * clipboard, we don't want to think that we still own it.
Karsten Hopp e373ee
+      */
Karsten Hopp e373ee
+     return FAIL;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Make vim NOT the owner of the current selection.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ /*ARGSUSED*/
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ clip_mch_lose_selection(VimClipboard *cbd)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     /* Nothing needs to be done here */
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Copy "str[*size]" into allocated memory, changing CR-NL to NL.
Karsten Hopp e373ee
+  * Return the allocated result and the size in "*size".
Karsten Hopp e373ee
+  * Returns NULL when out of memory.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     static char_u *
Karsten Hopp e373ee
+ crnl_to_nl(const char_u *str, int *size)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     int		pos = 0;
Karsten Hopp e373ee
+     int		str_len = *size;
Karsten Hopp e373ee
+     char_u	*ret;
Karsten Hopp e373ee
+     char_u	*retp;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Avoid allocating zero bytes, it generates an error message. */
Karsten Hopp e373ee
+     ret = lalloc((long_u)(str_len == 0 ? 1 : str_len), TRUE);
Karsten Hopp e373ee
+     if (ret != NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	retp = ret;
Karsten Hopp e373ee
+ 	for (pos = 0; pos < str_len; ++pos)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    if (str[pos] == '\r' && str[pos + 1] == '\n')
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		++pos;
Karsten Hopp e373ee
+ 		--(*size);
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    *retp++ = str[pos];
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     return ret;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Wait for another process to Close the Clipboard.
Karsten Hopp e373ee
+  * Returns TRUE for success.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     static int
Karsten Hopp e373ee
+ vim_open_clipboard(void)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     int delay = 10;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     while (!OpenClipboard(NULL))
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	if (delay > 500)
Karsten Hopp e373ee
+ 	    return FALSE;  /* waited too long, give up */
Karsten Hopp e373ee
+ 	Sleep(delay);
Karsten Hopp e373ee
+ 	delay *= 2;	/* wait for 10, 20, 40, 80, etc. msec */
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+     return TRUE;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Get the current selection and put it in the clipboard register.
Karsten Hopp e373ee
+  *
Karsten Hopp e373ee
+  * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
Karsten Hopp e373ee
+  * On NT/W95 the clipboard data is a fixed global memory object and
Karsten Hopp e373ee
+  * so its handle = its pointer.
Karsten Hopp e373ee
+  * On Win32s, however, co-operation with the Win16 system means that
Karsten Hopp e373ee
+  * the clipboard data is moveable and its handle is not a pointer at all,
Karsten Hopp e373ee
+  * so we can't just cast the return value of GetClipboardData to (char_u*).
Karsten Hopp e373ee
+  * <VN>
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ clip_mch_request_selection(VimClipboard *cbd)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     VimClipType_t	metadata = { -1, -1, -1, -1 };
Karsten Hopp e373ee
+     HGLOBAL		hMem = NULL;
Karsten Hopp e373ee
+     char_u		*str = NULL;
Karsten Hopp e373ee
+ #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     char_u		*to_free = NULL;
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ #ifdef FEAT_MBYTE
Karsten Hopp e373ee
+     HGLOBAL		rawh = NULL;
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+     int			str_size = 0;
Karsten Hopp e373ee
+     int			maxlen;
Karsten Hopp e373ee
+     size_t		n;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /*
Karsten Hopp e373ee
+      * Don't pass GetActiveWindow() as an argument to OpenClipboard() because
Karsten Hopp e373ee
+      * then we can't paste back into the same window for some reason - webb.
Karsten Hopp e373ee
+      */
Karsten Hopp e373ee
+     if (!vim_open_clipboard())
Karsten Hopp e373ee
+ 	return;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Check for vim's own clipboard format first.  This only gets the type of
Karsten Hopp e373ee
+      * the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
Karsten Hopp e373ee
+     if (IsClipboardFormatAvailable(cbd->format))
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	VimClipType_t	*meta_p;
Karsten Hopp e373ee
+ 	HGLOBAL		meta_h;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	/* We have metadata on the clipboard; try to get it. */
Karsten Hopp e373ee
+ 	if ((meta_h = GetClipboardData(cbd->format)) != NULL
Karsten Hopp e373ee
+ 		&& (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    /* The size of "VimClipType_t" changed, "rawlen" was added later.
Karsten Hopp e373ee
+ 	     * Only copy what is available for backwards compatibility. */
Karsten Hopp e373ee
+ 	    n = sizeof(VimClipType_t);
Karsten Hopp e373ee
+ 	    if (GlobalSize(meta_h) < n)
Karsten Hopp e373ee
+ 		n = GlobalSize(meta_h);
Karsten Hopp e373ee
+ 	    memcpy(&metadata, meta_p, n);
Karsten Hopp e373ee
+ 	    GlobalUnlock(meta_h);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #ifdef FEAT_MBYTE
Karsten Hopp e373ee
+     /* Check for Vim's raw clipboard format first.  This is used without
Karsten Hopp e373ee
+      * conversion, but only if 'encoding' matches. */
Karsten Hopp e373ee
+     if (IsClipboardFormatAvailable(cbd->format_raw)
Karsten Hopp e373ee
+ 				      && metadata.rawlen > (int)STRLEN(p_enc))
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	/* We have raw data on the clipboard; try to get it. */
Karsten Hopp e373ee
+ 	if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    char_u	*rawp;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	    rawp = (char_u *)GlobalLock(rawh);
Karsten Hopp e373ee
+ 	    if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		n = STRLEN(p_enc) + 1;
Karsten Hopp e373ee
+ 		str = rawp + n;
Karsten Hopp e373ee
+ 		str_size = (int)(metadata.rawlen - n);
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    else
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		GlobalUnlock(rawh);
Karsten Hopp e373ee
+ 		rawh = NULL;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+     if (str == NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     /* Try to get the clipboard in Unicode if it's not an empty string. */
Karsten Hopp e373ee
+     if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	HGLOBAL hMemW;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	    /* Use the length of our metadata if possible, but limit it to the
Karsten Hopp e373ee
+ 	     * GlobalSize() for safety. */
Karsten Hopp e373ee
+ 	    maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR));
Karsten Hopp e373ee
+ 	    if (metadata.ucslen >= 0)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		if (metadata.ucslen > maxlen)
Karsten Hopp e373ee
+ 		    str_size = maxlen;
Karsten Hopp e373ee
+ 		else
Karsten Hopp e373ee
+ 		    str_size = metadata.ucslen;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    else
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		for (str_size = 0; str_size < maxlen; ++str_size)
Karsten Hopp e373ee
+ 		    if (hMemWstr[str_size] == NUL)
Karsten Hopp e373ee
+ 			break;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size);
Karsten Hopp e373ee
+ 	    GlobalUnlock(hMemW);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+     else
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+     /* Get the clipboard in the Active codepage. */
Karsten Hopp e373ee
+     if (IsClipboardFormatAvailable(CF_TEXT))
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    str = (char_u *)GlobalLock(hMem);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	    /* The length is either what our metadata says or the strlen().
Karsten Hopp e373ee
+ 	     * But limit it to the GlobalSize() for safety. */
Karsten Hopp e373ee
+ 	    maxlen = (int)GlobalSize(hMem);
Karsten Hopp e373ee
+ 	    if (metadata.txtlen >= 0)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		if (metadata.txtlen > maxlen)
Karsten Hopp e373ee
+ 		    str_size = maxlen;
Karsten Hopp e373ee
+ 		else
Karsten Hopp e373ee
+ 		    str_size = metadata.txtlen;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    else
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		for (str_size = 0; str_size < maxlen; ++str_size)
Karsten Hopp e373ee
+ 		    if (str[str_size] == NUL)
Karsten Hopp e373ee
+ 			break;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+ 	    /* The text is in the active codepage.  Convert to 'encoding',
Karsten Hopp e373ee
+ 	     * going through UTF-16. */
Karsten Hopp e373ee
+ 	    acp_to_enc(str, str_size, &to_free, &maxlen);
Karsten Hopp e373ee
+ 	    if (to_free != NULL)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		str_size = maxlen;
Karsten Hopp e373ee
+ 		str = to_free;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ #ifdef FEAT_MBYTE
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     if (str != NULL && *str != NUL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	char_u *temp_clipboard;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	/* If the type is not known detect it. */
Karsten Hopp e373ee
+ 	if (metadata.type == -1)
Karsten Hopp e373ee
+ 	    metadata.type = MAUTO;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	/* Translate <CR><NL> into <NL>. */
Karsten Hopp e373ee
+ 	temp_clipboard = crnl_to_nl(str, &str_size);
Karsten Hopp e373ee
+ 	if (temp_clipboard != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
Karsten Hopp e373ee
+ 	    vim_free(temp_clipboard);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* unlock the global object */
Karsten Hopp e373ee
+     if (hMem != NULL)
Karsten Hopp e373ee
+ 	GlobalUnlock(hMem);
Karsten Hopp e373ee
+ #ifdef FEAT_MBYTE
Karsten Hopp e373ee
+     if (rawh != NULL)
Karsten Hopp e373ee
+ 	GlobalUnlock(rawh);
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+     CloseClipboard();
Karsten Hopp e373ee
+ #if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     vim_free(to_free);
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Send the current selection to the clipboard.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ clip_mch_set_selection(VimClipboard *cbd)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     char_u		*str = NULL;
Karsten Hopp e373ee
+     VimClipType_t	metadata;
Karsten Hopp e373ee
+     long_u		txtlen;
Karsten Hopp e373ee
+     HGLOBAL		hMemRaw = NULL;
Karsten Hopp e373ee
+     HGLOBAL		hMem = NULL;
Karsten Hopp e373ee
+     HGLOBAL		hMemVim = NULL;
Karsten Hopp e373ee
+ # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     HGLOBAL		hMemW = NULL;
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* If the '*' register isn't already filled in, fill it in now */
Karsten Hopp e373ee
+     cbd->owned = TRUE;
Karsten Hopp e373ee
+     clip_get_selection(cbd);
Karsten Hopp e373ee
+     cbd->owned = FALSE;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Get the text to be put on the clipboard, with CR-LF. */
Karsten Hopp e373ee
+     metadata.type = clip_convert_selection(&str, &txtlen, cbd);
Karsten Hopp e373ee
+     if (metadata.type < 0)
Karsten Hopp e373ee
+ 	return;
Karsten Hopp e373ee
+     metadata.txtlen = (int)txtlen;
Karsten Hopp e373ee
+     metadata.ucslen = 0;
Karsten Hopp e373ee
+     metadata.rawlen = 0;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #ifdef FEAT_MBYTE
Karsten Hopp e373ee
+     /* Always set the raw bytes: 'encoding', NUL and the text.  This is used
Karsten Hopp e373ee
+      * when copy/paste from/to Vim with the same 'encoding', so that illegal
Karsten Hopp e373ee
+      * bytes can also be copied and no conversion is needed. */
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	LPSTR lpszMemRaw;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1);
Karsten Hopp e373ee
+ 	hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
Karsten Hopp e373ee
+ 							 metadata.rawlen + 1);
Karsten Hopp e373ee
+ 	lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
Karsten Hopp e373ee
+ 	if (lpszMemRaw != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    STRCPY(lpszMemRaw, p_enc);
Karsten Hopp e373ee
+ 	    memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
Karsten Hopp e373ee
+ 	    GlobalUnlock(hMemRaw);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	else
Karsten Hopp e373ee
+ 	    metadata.rawlen = 0;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	WCHAR		*out;
Karsten Hopp e373ee
+ 	int		len = metadata.txtlen;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	/* Convert the text to UTF-16. This is put on the clipboard as
Karsten Hopp e373ee
+ 	 * CF_UNICODETEXT. */
Karsten Hopp e373ee
+ 	out = (WCHAR *)enc_to_utf16(str, &len;;
Karsten Hopp e373ee
+ 	if (out != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    WCHAR *lpszMemW;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	    /* Convert the text for CF_TEXT to Active codepage. Otherwise it's
Karsten Hopp e373ee
+ 	     * p_enc, which has no relation to the Active codepage. */
Karsten Hopp e373ee
+ 	    metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
Karsten Hopp e373ee
+ 							       NULL, 0, 0, 0);
Karsten Hopp e373ee
+ 	    vim_free(str);
Karsten Hopp e373ee
+ 	    str = (char_u *)alloc((unsigned)(metadata.txtlen == 0 ? 1
Karsten Hopp e373ee
+ 							  : metadata.txtlen));
Karsten Hopp e373ee
+ 	    if (str == NULL)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		vim_free(out);
Karsten Hopp e373ee
+ 		return;		/* out of memory */
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    WideCharToMultiByte(GetACP(), 0, out, len,
Karsten Hopp e373ee
+ 						  str, metadata.txtlen, 0, 0);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	    /* Allocate memory for the UTF-16 text, add one NUL word to
Karsten Hopp e373ee
+ 	     * terminate the string. */
Karsten Hopp e373ee
+ 	    hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
Karsten Hopp e373ee
+ 						   (len + 1) * sizeof(WCHAR));
Karsten Hopp e373ee
+ 	    lpszMemW = (WCHAR *)GlobalLock(hMemW);
Karsten Hopp e373ee
+ 	    if (lpszMemW != NULL)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		memcpy(lpszMemW, out, len * sizeof(WCHAR));
Karsten Hopp e373ee
+ 		lpszMemW[len] = NUL;
Karsten Hopp e373ee
+ 		GlobalUnlock(hMemW);
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ 	    vim_free(out);
Karsten Hopp e373ee
+ 	    metadata.ucslen = len;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Allocate memory for the text, add one NUL byte to terminate the string.
Karsten Hopp e373ee
+      */
Karsten Hopp e373ee
+     hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	if (lpszMem)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    vim_strncpy(lpszMem, str, metadata.txtlen);
Karsten Hopp e373ee
+ 	    GlobalUnlock(hMem);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Set up metadata: */
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	VimClipType_t *lpszMemVim = NULL;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
Karsten Hopp e373ee
+ 						       sizeof(VimClipType_t));
Karsten Hopp e373ee
+ 	lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
Karsten Hopp e373ee
+ 	memcpy(lpszMemVim, &metadata, sizeof(metadata));
Karsten Hopp e373ee
+ 	GlobalUnlock(hMemVim);
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /*
Karsten Hopp e373ee
+      * Open the clipboard, clear it and put our text on it.
Karsten Hopp e373ee
+      * Always set our Vim format.  Put Unicode and plain text on it.
Karsten Hopp e373ee
+      *
Karsten Hopp e373ee
+      * Don't pass GetActiveWindow() as an argument to OpenClipboard()
Karsten Hopp e373ee
+      * because then we can't paste back into the same window for some
Karsten Hopp e373ee
+      * reason - webb.
Karsten Hopp e373ee
+      */
Karsten Hopp e373ee
+     if (vim_open_clipboard())
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	if (EmptyClipboard())
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    SetClipboardData(cbd->format, hMemVim);
Karsten Hopp e373ee
+ 	    hMemVim = 0;
Karsten Hopp e373ee
+ # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+ 	    if (hMemW != NULL)
Karsten Hopp e373ee
+ 	    {
Karsten Hopp e373ee
+ 		if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
Karsten Hopp e373ee
+ 		    hMemW = NULL;
Karsten Hopp e373ee
+ 	    }
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+ 	    /* Always use CF_TEXT.  On Win98 Notepad won't obtain the
Karsten Hopp e373ee
+ 	     * CF_UNICODETEXT text, only CF_TEXT. */
Karsten Hopp e373ee
+ 	    SetClipboardData(CF_TEXT, hMem);
Karsten Hopp e373ee
+ 	    hMem = 0;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	CloseClipboard();
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     vim_free(str);
Karsten Hopp e373ee
+     /* Free any allocations we didn't give to the clipboard: */
Karsten Hopp e373ee
+     if (hMemRaw)
Karsten Hopp e373ee
+ 	GlobalFree(hMemRaw);
Karsten Hopp e373ee
+     if (hMem)
Karsten Hopp e373ee
+ 	GlobalFree(hMem);
Karsten Hopp e373ee
+ # if defined(FEAT_MBYTE) && defined(WIN3264)
Karsten Hopp e373ee
+     if (hMemW)
Karsten Hopp e373ee
+ 	GlobalFree(hMemW);
Karsten Hopp e373ee
+ # endif
Karsten Hopp e373ee
+     if (hMemVim)
Karsten Hopp e373ee
+ 	GlobalFree(hMemVim);
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #endif /* FEAT_CLIPBOARD */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #if defined(FEAT_MBYTE) || defined(PROTO)
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Note: the following two functions are only guaranteed to work when using
Karsten Hopp e373ee
+  * valid MS-Windows codepages or when iconv() is available.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Convert "str" from 'encoding' to UTF-16.
Karsten Hopp e373ee
+  * Input in "str" with length "*lenp".  When "lenp" is NULL, use strlen().
Karsten Hopp e373ee
+  * Output is returned as an allocated string.  "*lenp" is set to the length of
Karsten Hopp e373ee
+  * the result.  A trailing NUL is always added.
Karsten Hopp e373ee
+  * Returns NULL when out of memory.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     short_u *
Karsten Hopp e373ee
+ enc_to_utf16(char_u *str, int *lenp)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     vimconv_T	conv;
Karsten Hopp e373ee
+     WCHAR	*ret;
Karsten Hopp e373ee
+     char_u	*allocbuf = NULL;
Karsten Hopp e373ee
+     int		len_loc;
Karsten Hopp e373ee
+     int		length;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     if (lenp == NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	len_loc = (int)STRLEN(str) + 1;
Karsten Hopp e373ee
+ 	lenp = &len_loc;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     if (enc_codepage > 0)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	/* We can do any CP### -> UTF-16 in one pass, and we can do it
Karsten Hopp e373ee
+ 	 * without iconv() (convert_* may need iconv). */
Karsten Hopp e373ee
+ 	MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+     else
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	/* Use "latin1" by default, we might be called before we have p_enc
Karsten Hopp e373ee
+ 	 * set up.  Convert to utf-8 first, works better with iconv().  Does
Karsten Hopp e373ee
+ 	 * nothing if 'encoding' is "utf-8". */
Karsten Hopp e373ee
+ 	conv.vc_type = CONV_NONE;
Karsten Hopp e373ee
+ 	if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
Karsten Hopp e373ee
+ 						   (char_u *)"utf-8") == FAIL)
Karsten Hopp e373ee
+ 	    return NULL;
Karsten Hopp e373ee
+ 	if (conv.vc_type != CONV_NONE)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    str = allocbuf = string_convert(&conv, str, lenp);
Karsten Hopp e373ee
+ 	    if (str == NULL)
Karsten Hopp e373ee
+ 		return NULL;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	convert_setup(&conv, NULL, NULL);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	length = utf8_to_utf16(str, *lenp, NULL, NULL);
Karsten Hopp e373ee
+ 	ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
Karsten Hopp e373ee
+ 	if (ret != NULL)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
Karsten Hopp e373ee
+ 	    ret[length] = 0;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	vim_free(allocbuf);
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     *lenp = length;
Karsten Hopp e373ee
+     return (short_u *)ret;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Convert an UTF-16 string to 'encoding'.
Karsten Hopp e373ee
+  * Input in "str" with length (counted in wide characters) "*lenp".  When
Karsten Hopp e373ee
+  * "lenp" is NULL, use wcslen().
Karsten Hopp e373ee
+  * Output is returned as an allocated string.  If "*lenp" is not NULL it is
Karsten Hopp e373ee
+  * set to the length of the result.
Karsten Hopp e373ee
+  * Returns NULL when out of memory.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     char_u *
Karsten Hopp e373ee
+ utf16_to_enc(short_u *str, int *lenp)
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     vimconv_T	conv;
Karsten Hopp e373ee
+     char_u	*utf8_str = NULL, *enc_str = NULL;
Karsten Hopp e373ee
+     int		len_loc;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     if (lenp == NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	len_loc = (int)wcslen(str) + 1;
Karsten Hopp e373ee
+ 	lenp = &len_loc;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     if (enc_codepage > 0)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	/* We can do any UTF-16 -> CP### in one pass. */
Karsten Hopp e373ee
+ 	int length;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
Karsten Hopp e373ee
+ 					    (LPSTR *)&enc_str, &length, 0, 0);
Karsten Hopp e373ee
+ 	*lenp = length;
Karsten Hopp e373ee
+ 	return enc_str;
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     /* Avoid allocating zero bytes, it generates an error message. */
Karsten Hopp e373ee
+     utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
Karsten Hopp e373ee
+     if (utf8_str != NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	*lenp = utf16_to_utf8(str, *lenp, utf8_str);
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	/* We might be called before we have p_enc set up. */
Karsten Hopp e373ee
+ 	conv.vc_type = CONV_NONE;
Karsten Hopp e373ee
+ 	convert_setup(&conv, (char_u *)"utf-8",
Karsten Hopp e373ee
+ 					    p_enc? p_enc: (char_u *)"latin1");
Karsten Hopp e373ee
+ 	if (conv.vc_type == CONV_NONE)
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    /* p_enc is utf-8, so we're done. */
Karsten Hopp e373ee
+ 	    enc_str = utf8_str;
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 	else
Karsten Hopp e373ee
+ 	{
Karsten Hopp e373ee
+ 	    enc_str = string_convert(&conv, utf8_str, lenp);
Karsten Hopp e373ee
+ 	    vim_free(utf8_str);
Karsten Hopp e373ee
+ 	}
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ 	convert_setup(&conv, NULL, NULL);
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     return enc_str;
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ #endif /* FEAT_MBYTE */
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ #if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
Karsten Hopp e373ee
+ /*
Karsten Hopp e373ee
+  * Convert from the active codepage to 'encoding'.
Karsten Hopp e373ee
+  * Input is "str[str_size]".
Karsten Hopp e373ee
+  * The result is in allocated memory: "out[outlen]".  With terminating NUL.
Karsten Hopp e373ee
+  */
Karsten Hopp e373ee
+     void
Karsten Hopp e373ee
+ acp_to_enc(str, str_size, out, outlen)
Karsten Hopp e373ee
+     char_u	*str;
Karsten Hopp e373ee
+     int		str_size;
Karsten Hopp e373ee
+     char_u	**out;
Karsten Hopp e373ee
+     int		*outlen;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+ {
Karsten Hopp e373ee
+     LPWSTR	widestr;
Karsten Hopp e373ee
+ 
Karsten Hopp e373ee
+     MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
Karsten Hopp e373ee
+     if (widestr != NULL)
Karsten Hopp e373ee
+     {
Karsten Hopp e373ee
+ 	++*outlen;	/* Include the 0 after the string */
Karsten Hopp e373ee
+ 	*out = utf16_to_enc((short_u *)widestr, outlen);
Karsten Hopp e373ee
+ 	vim_free(widestr);
Karsten Hopp e373ee
+     }
Karsten Hopp e373ee
+ }
Karsten Hopp e373ee
+ #endif
Karsten Hopp e373ee
*** ../vim-7.3.835/src/version.c	2013-02-26 14:14:01.000000000 +0100
Karsten Hopp e373ee
--- src/version.c	2013-02-26 14:43:36.000000000 +0100
Karsten Hopp e373ee
***************
Karsten Hopp e373ee
*** 730,731 ****
Karsten Hopp e373ee
--- 730,733 ----
Karsten Hopp e373ee
  {   /* Add new patch number below this line */
Karsten Hopp e373ee
+ /**/
Karsten Hopp e373ee
+     836,
Karsten Hopp e373ee
  /**/
Karsten Hopp e373ee
Karsten Hopp e373ee
-- 
Karsten Hopp e373ee
Engineers understand that their appearance only bothers other people and
Karsten Hopp e373ee
therefore it is not worth optimizing.
Karsten Hopp e373ee
				(Scott Adams - The Dilbert principle)
Karsten Hopp e373ee
Karsten Hopp e373ee
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp e373ee
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp e373ee
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp e373ee
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///