*** select-msw.c.old	Tue Jun  2 04:50:58 1998
--- select-msw.c	Mon Jul  3 20:19:11 2000
***************
*** 32,90 ****
  
  #include "console-msw.h"
  
! DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
! Copy STRING to the mswindows clipboard.
  */
!        (string))
  {
!   int rawsize, size, i;
    unsigned char *src, *dst, *next;
    HGLOBAL h = NULL;
  
    CHECK_STRING (string);
  
    /* Calculate size with LFs converted to CRLFs because
     * CF_TEXT format uses CRLF delimited ASCIIZ */
    src = XSTRING_DATA (string);
    size = rawsize = XSTRING_LENGTH (string) + 1;
!   for (i=0; i<rawsize; i++)
!     if (src[i] == '\n')
!       size++;
! 
    if (!OpenClipboard (NULL))
      return Qnil;
  
!   if (!EmptyClipboard () ||
!       (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
        (dst = (unsigned char *) GlobalLock (h)) == NULL)
      {
        if (h != NULL) GlobalFree (h);
        CloseClipboard ();
        return Qnil;
      }
!     
!   /* Convert LFs to CRLFs */
!   do
      {
!       /* copy next line or remaining bytes including '\0' */
!       next = memccpy (dst, src, '\n', rawsize);
!       if (next)
  	{
! 	  /* copied one line ending with '\n' */
! 	  int copied = next - dst;
! 	  rawsize -= copied;
! 	  src += copied;
! 	  /* insert '\r' before '\n' */
! 	  next[-1] = '\r';
! 	  next[0] = '\n';
! 	  dst = next+1;
! 	}	    
      }
!   while (next);
      
    GlobalUnlock (h);
    
!   i = (SetClipboardData (CF_TEXT, h) != NULL);
    
    CloseClipboard ();
    GlobalFree (h);
--- 32,134 ----
  
  #include "console-msw.h"
  
! DEFUN ("mswindows-register-clipboard-format",
!        Fmswindows_register_clipboard_format,
!        1, 1, 0, /*
! Register a new mswindows clipboard format with the given NAME.
  */
!        (name))
  {
!   unsigned char *name_sz;
!   
!   CHECK_STRING (name);
!   
!   name_sz = XSTRING_DATA (name);
!   
!   return RegisterClipboardFormat (name_sz);
! }
! 
! DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 4, 0, /*
! Copy STRING to the mswindows clipboard, using the specified clipboard FORMAT,
! or plain text if no format is specified. The current data on the clipboard is
! replaced if NOREPLACE is nil. If NOTRANSLATE is nil, then CRLF conversion is
! performed.
! */
!        (string, format, noreplace, notranslate))
! {
!   int rawsize, size, i, fmt;
    unsigned char *src, *dst, *next;
    HGLOBAL h = NULL;
  
    CHECK_STRING (string);
  
+   if (NILP (format))
+     fmt = CF_TEXT;
+   else
+     {
+       CHECK_INT (format);
+       fmt = XINT (format);
+     }
+   
    /* Calculate size with LFs converted to CRLFs because
     * CF_TEXT format uses CRLF delimited ASCIIZ */
    src = XSTRING_DATA (string);
    size = rawsize = XSTRING_LENGTH (string) + 1;
!   
!   if (NILP (notranslate))
!     {
!       for (i=0; i<rawsize; i++)
! 	if (src[i] == '\n')
! 	  size++;
!     }
!       
    if (!OpenClipboard (NULL))
      return Qnil;
  
!   if (NILP (noreplace))
!     {
!       if (!EmptyClipboard ())
! 	{
! 	  CloseClipboard ();
! 	  return Qnil;
! 	}
!     }
!   
!   if ((h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
        (dst = (unsigned char *) GlobalLock (h)) == NULL)
      {
        if (h != NULL) GlobalFree (h);
        CloseClipboard ();
        return Qnil;
      }
! 
!   if (NILP (notranslate))
      {
!       /* Convert LFs to CRLFs */
!       do
  	{
! 	  /* copy next line or remaining bytes including '\0' */
! 	  next = memccpy (dst, src, '\n', rawsize);
! 	  if (next)
! 	    {
! 	      /* copied one line ending with '\n' */
! 	      int copied = next - dst;
! 	      rawsize -= copied;
! 	      src += copied;
! 	      /* insert '\r' before '\n' */
! 	      next[-1] = '\r';
! 	      next[0] = '\n';
! 	      dst = next+1;
! 	    }	    
! 	}
!       while (next);
      }
!   else
!     memcpy (dst, src, rawsize);
      
    GlobalUnlock (h);
    
!   i = (SetClipboardData (fmt, h) != NULL);
    
    CloseClipboard ();
    GlobalFree (h);
***************
*** 92,141 ****
    return i ? Qt : Qnil;
  }
  
! DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
! Return the contents of the mswindows clipboard.
  */
!        ())
  {
    HANDLE h;
    unsigned char *src, *dst, *next;
    Lisp_Object ret = Qnil;
  
    if (!OpenClipboard (NULL))
      return Qnil;
  
!   if ((h = GetClipboardData (CF_TEXT)) != NULL &&
        (src = (unsigned char *) GlobalLock (h)) != NULL)
      {
        int i;
        int size, rawsize;
        size = rawsize = strlen (src);
  
!       for (i=0; i<rawsize; i++)
! 	if (src[i] == '\r' && src[i+1] == '\n')
! 	  size--;
! 
        /* Convert CRLFs to LFs */
        ret = make_uninit_string (size);
        dst = XSTRING_DATA (ret);
!       do
  	{
! 	  /* copy next line or remaining bytes excluding '\0' */
! 	  next = memccpy (dst, src, '\r', rawsize);
! 	  if (next)
  	    {
! 	      /* copied one line ending with '\r' */
! 	      int copied = next - dst;
! 	      rawsize -= copied;
! 	      src += copied;
! 	      if (*src == '\n')
! 		dst += copied - 1;		/* overwrite '\r' */
! 	      else
! 		dst += copied;
! 	    }	    
  	}
!       while (next);
! 
        GlobalUnlock (h);
      }
  
--- 136,205 ----
    return i ? Qt : Qnil;
  }
  
! DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 2, 0, /*
! Return the contents of the mswindows clipboard, optionally in the specified
! FORMAT (otherwise in plain text). If NOTRANSLATE is nil, CRLF translation
! is performed.
  */
!        (format, notranslate))
  {
    HANDLE h;
+   int fmt;
    unsigned char *src, *dst, *next;
    Lisp_Object ret = Qnil;
  
+   if (NILP (format))
+     fmt = CF_TEXT;
+   else
+     {
+       CHECK_INT (format);
+       fmt = XINT (format);
+     }
+   
    if (!OpenClipboard (NULL))
      return Qnil;
  
!   if ((h = GetClipboardData (fmt)) != NULL &&
        (src = (unsigned char *) GlobalLock (h)) != NULL)
      {
        int i;
        int size, rawsize;
        size = rawsize = strlen (src);
  
!       if (NILP (notranslate))
! 	{
! 	  for (i=0; i<rawsize; i++)
! 	    if (src[i] == '\r' && src[i+1] == '\n')
! 	      size--;
! 	}
!       
        /* Convert CRLFs to LFs */
        ret = make_uninit_string (size);
        dst = XSTRING_DATA (ret);
! 
!       if (NILP (notranslate))
  	{
! 	  do
  	    {
! 	      /* copy next line or remaining bytes excluding '\0' */
! 	      next = memccpy (dst, src, '\r', rawsize);
! 	      if (next)
! 		{
! 		  /* copied one line ending with '\r' */
! 		  int copied = next - dst;
! 		  rawsize -= copied;
! 		  src += copied;
! 		  if (*src == '\n')
! 		    dst += copied - 1;		/* overwrite '\r' */
! 		  else
! 		    dst += copied;
! 		}	    
! 	    }
! 	  while (next);
  	}
!       else
! 	memcpy (dst, src, rawsize);
!       
        GlobalUnlock (h);
      }
  
***************
*** 144,155 ****
    return ret;
  }
  
! DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
! Whether there is an MS-Windows selection.
  */
!        ())
  {
!   return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
  }
  
  DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
--- 208,230 ----
    return ret;
  }
  
! DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 1, 0, /*
! Whether there is an MS-Windows selection. If FORMAT is specified, look for a
! selection in the specified clipboard format.
  */
!        (format))
  {
!   int fmt;
! 
!   if (NILP (format))
!     fmt = CF_TEXT;
!   else
!     {
!       CHECK_INT (format);
!       fmt = XINT (format);
!     }
!   
!   return IsClipboardFormatAvailable (fmt) ? Qt : Qnil;
  }
  
  DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
***************
*** 172,177 ****
--- 247,253 ----
    DEFSUBR (Fmswindows_get_clipboard);
    DEFSUBR (Fmswindows_selection_exists_p);
    DEFSUBR (Fmswindows_delete_selection);
+   DEFSUBR (Fmswindows_register_clipboard_format);
  }
  
  void

