changeset 22729:3537e1563a66

(Vclipboard_coding_system): New variable. (set_clipboard_data, set_clipboard_data): New parameter Raw determines whether CRLF <-> NL translation needs to be done. All callers changed. (Fw16_set_clipboard_data): Encode the text using Vclipboard_coding_system, if necessary. (Fw16_get_clipboard_data): Decode the text using Vclipboard_coding_system, if necessary. (syms_of_win16select): DEFVAR Vclipboard_coding_system and staticpro it.
author Eli Zaretskii <eliz@gnu.org>
date Thu, 09 Jul 1998 14:02:15 +0000
parents 4596d7f197d8
children 0fb112106d3e
files src/w16select.c
diffstat 1 files changed, 137 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/w16select.c	Thu Jul 09 05:32:47 1998 +0000
+++ b/src/w16select.c	Thu Jul 09 14:02:15 1998 +0000
@@ -37,6 +37,9 @@
 #include "dispextern.h"	/* frame.h seems to want this */
 #include "frame.h"	/* Need this to get the X window of selected_frame */
 #include "blockinput.h"
+#include "buffer.h"
+#include "charset.h"
+#include "coding.h"
 
 /* If ever some function outside this file will need to call any
    clipboard-related function, the following prototypes and constants
@@ -57,14 +60,18 @@
 unsigned identify_winoldap_version (void);
 unsigned open_clipboard (void);
 unsigned empty_clipboard (void);
-unsigned set_clipboard_data (unsigned, void *, unsigned);
+unsigned set_clipboard_data (unsigned, void *, unsigned, int);
 unsigned get_clipboard_data_size (unsigned);
-unsigned get_clipboard_data (unsigned, void *, unsigned);
+unsigned get_clipboard_data (unsigned, void *, unsigned, int);
 unsigned close_clipboard (void);
 unsigned clipboard_compact (unsigned);
 
 Lisp_Object QCLIPBOARD, QPRIMARY;
 
+/* Coding system for communicating with other Windows programs via the
+   clipboard.  */
+static Lisp_Object Vclipboard_coding_system;
+
 /* The segment address and the size of the buffer in low
    memory used to move data between us and WinOldAp module.  */
 
@@ -216,10 +223,11 @@
 
 /* Copy data into the clipboard, return non-zero if successfull.  */
 unsigned
-set_clipboard_data (Format, Data, Size)
+set_clipboard_data (Format, Data, Size, Raw)
      unsigned Format;
      void *Data;
      unsigned Size;
+     int Raw;
 {
   __dpmi_regs regs;
   unsigned truelen;
@@ -233,11 +241,15 @@
      standard CF_TEXT clipboard format uses CRLF line endings,
      while Emacs uses just LF internally).  */
   truelen = Size;
-  /* avoid using strchr because it recomputes the length everytime */
-  while ((dp = memchr (dp, '\n', Size - (dp - dstart))) != 0)
+
+  if (!Raw)
     {
-      truelen++;
-      dp++;
+      /* avoid using strchr because it recomputes the length everytime */
+      while ((dp = memchr (dp, '\n', Size - (dp - dstart))) != 0)
+	{
+	  truelen++;
+	  dp++;
+	}
     }
 
   if (clipboard_compact (truelen) < truelen)
@@ -246,15 +258,20 @@
   if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0)
     return 0;
 
-  /* Move the buffer into the low memory, convert LF into CR-LF pairs.  */
-  dp = Data;
-  buf_offset = xbuf_addr;
-  _farsetsel (_dos_ds);
-  while (Size--)
+  /* Move the buffer into the low memory, convert LF into CR-LF if needed.  */
+  if (Raw)
+    dosmemput (Data, truelen, __tb);
+  else
     {
-      if (*dp == '\n')
-	_farnspokeb (buf_offset++, '\r');
-      _farnspokeb (buf_offset++, *dp++);
+      dp = Data;
+      buf_offset = xbuf_addr;
+      _farsetsel (_dos_ds);
+      while (Size--)
+	{
+	  if (*dp == '\n')
+	    _farnspokeb (buf_offset++, '\r');
+	  _farnspokeb (buf_offset++, *dp++);
+	}
     }
 
   /* Calls Int 2Fh/AX=1703h with:
@@ -299,10 +316,11 @@
    Warning: this doesn't check whether DATA has enough space to hold
    SIZE bytes.  */
 unsigned
-get_clipboard_data (Format, Data, Size)
+get_clipboard_data (Format, Data, Size, Raw)
      unsigned Format;
      void *Data;
      unsigned Size;
+     int Raw;
 {
   __dpmi_regs regs;
   unsigned datalen = 0;
@@ -331,13 +349,14 @@
   __dpmi_int(0x2f, &regs);
   if (regs.x.ax != 0)
     {
-      /* Copy data from low memory, remove CR characters if before LF.  */
+      /* Copy data from low memory, remove CR
+	 characters before LF if needed.  */
       _farsetsel (_dos_ds);
       while (Size--)
 	{
 	  register unsigned char c = _farnspeekb (xbuf_addr++);
 
-	  if ((*dp++ = c) == '\r' && _farnspeekb (xbuf_addr) == '\n')
+	  if ((*dp++ = c) == '\r' && !Raw && _farnspeekb (xbuf_addr) == '\n')
 	    {
 	      dp--;
 	      *dp++ = '\n';
@@ -401,7 +420,10 @@
 {
   int ok = 1, ok1 = 1;
   int nbytes;
-  unsigned char *src;
+  unsigned char *src, *dst = NULL;
+  int charsets[MAX_CHARSET + 1];
+  int num;
+  int no_crlf_conversion;
 
   CHECK_STRING (string, 0);
   
@@ -414,14 +436,50 @@
   
   BLOCK_INPUT;
 
-  nbytes = XSTRING (string)->size + 1;
+  nbytes = STRING_BYTES (XSTRING (string)) + 1;
   src = XSTRING (string)->data;
-    
+
+  /* Since we are now handling multilingual text, we must consider
+     encoding text for the clipboard.  */
+
+  bzero (charsets, (MAX_CHARSET + 1) * sizeof (int));
+  num = ((nbytes <= 2	/* Check the possibility of short cut.  */
+	  || NILP (buffer_defaults.enable_multibyte_characters))
+	 ? 0
+	 : find_charset_in_str (src, nbytes, charsets, Qnil, 1));
+
+  if (!num || (num == 1 && charsets[CHARSET_ASCII]))
+    {
+      /* No multibyte character in OBJ.  We need not encode it, but we
+	 will have to convert it to DOS CR-LF style.  */
+      no_crlf_conversion = 0;
+    }
+  else
+    {
+      /* We must encode contents of STRING according to what
+	 clipboard-coding-system specifies.  */
+      int bufsize;
+      struct coding_system coding;
+      unsigned char *htext2;
+
+      setup_coding_system
+	(Fcheck_coding_system (Vclipboard_coding_system), &coding);
+      coding.mode |= CODING_MODE_LAST_BLOCK;
+      Vlast_coding_system_used = coding.symbol;
+      bufsize = encoding_buffer_size (&coding, nbytes);
+      dst = (unsigned char *) xmalloc (bufsize);
+      encode_coding (&coding, src, dst, nbytes, bufsize);
+      no_crlf_conversion = 1;
+    }
+
   if (!open_clipboard ())
     goto error;
   
-  ok = empty_clipboard () && (ok1 = set_clipboard_data (CF_TEXT, src, nbytes));
-  
+  ok = empty_clipboard ()
+    && (ok1 = set_clipboard_data (CF_TEXT, src, nbytes, no_crlf_conversion));
+
+  if (!no_crlf_conversion)
+    Vlast_coding_system_used = Qraw_text;
   close_clipboard ();
   
   if (ok) goto unblock;
@@ -431,6 +489,8 @@
   ok = 0;
 
  unblock:
+  if (dst)
+    xfree (dst);
   UNBLOCK_INPUT;
 
   /* Notify user if the text is too large to fit into DOS memory.
@@ -457,10 +517,9 @@
   unsigned data_size, truelen;
   unsigned char *htext;
   Lisp_Object ret = Qnil;
-  
-  if (!NILP (frame))
-    CHECK_LIVE_FRAME (frame, 0);
-  
+  int no_crlf_conversion;
+  int require_encoding = 0;
+
   if (NILP (frame))
     frame = Fselected_frame ();
 
@@ -480,10 +539,51 @@
   /* need to know final size after '\r' chars are removed because
      we can't change the string size manually, and doing an extra
      copy is silly */
-  if ((truelen = get_clipboard_data (CF_TEXT, htext, data_size)) == 0)
+  if ((truelen = get_clipboard_data (CF_TEXT, htext, data_size, 0)) == 0)
     goto closeclip;
 
-  ret = make_string (htext, truelen);
+  /* Do we need to decode it?  */
+  if (! NILP (buffer_defaults.enable_multibyte_characters))
+    {
+      /* If the clipboard data contains any 8-bit Latin-1 code, we
+	 need to decode it.  */
+      int i;
+
+      for (i = 0; i < truelen; i++)
+	{
+	  if (htext[i] >= 0x80)
+	    {
+	      require_encoding = 1;
+	      break;
+	    }
+	}
+    }
+  if (require_encoding)
+    {
+      int bufsize;
+      unsigned char *buf;
+      struct coding_system coding;
+
+      setup_coding_system
+	(Fcheck_coding_system (Vclipboard_coding_system), &coding);
+      coding.mode |= CODING_MODE_LAST_BLOCK;
+      truelen = get_clipboard_data (CF_TEXT, htext, data_size, 1);
+      bufsize = decoding_buffer_size (&coding, truelen);
+      buf = (unsigned char *) xmalloc (bufsize);
+      decode_coding (&coding, htext, buf, truelen, bufsize);
+      truelen = (coding.fake_multibyte
+		 ? multibyte_chars_in_text (buf, coding.produced)
+		 : coding.produced_char);
+      ret = make_string_from_bytes ((char *) buf, truelen, coding.produced);
+      xfree (buf);
+      Vlast_coding_system_used = coding.symbol;
+    }
+  else
+    {
+      ret = make_unibyte_string ((char *) htext, truelen);
+      Vlast_coding_system_used = Qraw_text;
+    }
+
   xfree (htext);
 
  closeclip:
@@ -549,6 +649,14 @@
   defsubr (&Sw16_get_clipboard_data);
   defsubr (&Sx_selection_exists_p);
 
+  DEFVAR_LISP ("clipboard-coding-system", &Vclipboard_coding_system,
+    "Coding system for communicating with other X clients.\n\
+When sending or receiving text via cut_buffer, selection, and clipboard,\n\
+the text is encoded or decoded by this coding system.\n\
+A default value is `iso-latin-1-dos'");
+  Vclipboard_coding_system=intern ("iso-latin-1-dos");
+  staticpro(&Vclipboard_coding_system);
+
   QPRIMARY   = intern ("PRIMARY");	staticpro (&QPRIMARY);
   QCLIPBOARD = intern ("CLIPBOARD");	staticpro (&QCLIPBOARD);
 }