unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [Patch] Unicode support for the MS Windows clipboard
@ 2004-05-26 18:01 Benjamin Riefenstahl
  2004-05-27  7:58 ` Jason Rumney
  2004-05-27  8:05 ` [Patch] " Eli Zaretskii
  0 siblings, 2 replies; 72+ messages in thread
From: Benjamin Riefenstahl @ 2004-05-26 18:01 UTC (permalink / raw)
  Cc: Sam Steingold

[-- Attachment #1: Type: text/plain, Size: 2412 bytes --]

Hi everybody,


As we just had another discussion on this, I sat down and wrote a
preliminary implementation of Unicode support for the MS Windows
clipboard, see attached patch.

I assume some of you have opinions on how this should be actually be
packaged, so this patch is mostly for introduction, testing and
playing around.  If this code is acceptable in principle, I will
probably also need to send in papers for the copyright assignment,
before it can be used.


What does it do:

- Introduce a new variable `w32-clipboard-type' to use with
  cut-and-paste instead of the hard-coded CF_TEXT.  The default for
  `w32-clipboard-type' is CF_TEXT, because CF_UNICODETEXT is not
  compatible with 9x/Me, it uses more memory and CF_TEXT was used
  before.

- Drop optimizations for ASCII-only text.  This is mostly because I
  couldn't get all combinations straight in my mind, between this, the
  `last_clipboard_text' mechanism and CF_UNICODETEXT.


Open questions:

- Support for CF_OEMTEXT (console text) in the clipboard may look
  superfluous.  OTOH this may be usefull for full console mode support
  (emacs -nw) on 9x/Me.  If we keep this, the default for
  `w32-clipboard-type' should depend on the display mode (console vs
  GUI).  In that case `w32-clipboard-type' should be set from the Lisp
  code, though, I think.

- `selection-coding-system' and `w32-clipboard-type' need to be set in
  a synchronized manner.  I'm not yet sure if we want this to be done
  by Lisp code or by combining/synchronizing the two variables in the
  C code somehow.

  If we keep them separate, users can in theory use other
  coding-systems with this as they see fit.  They can adapt it to
  whatever exotic locales and coding-systems that they may have.  But
  that may be an academic issue.

  Anyway, as long as they are kept separate, these are the recommended
  combinations for an English version of Windows:

      w32-clipboard-type | selection-coding-system
      --------------------------------------------
      CF_TEXT            | cp1252-dos (GetACP())
      CF_OEMTEXT         | cp850-dos (GetOEMCP())
      CF_UNICODETEXT     | utf-16le-dos

- If we keep `w32-clipboard-type', we could try to use more
  user-oriented names for the different types instead of just taking
  them from the C API names.

- We want to either drop the #if 0 sections in the code completly or
  make that code work.


benny


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: w32select.c.patch --]
[-- Type: text/x-patch, Size: 6725 bytes --]

Index: w32select.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/w32select.c,v
retrieving revision 1.32
diff -u -p -r1.32 w32select.c
--- w32select.c	18 Apr 2004 18:34:03 -0000	1.32
+++ w32select.c	26 May 2004 17:35:14 -0000
@@ -41,6 +41,10 @@ static Lisp_Object Vselection_coding_sys
 /* Coding system for the next communicating with other Windows programs.  */
 static Lisp_Object Vnext_selection_coding_system;
 
+/* Type of clipboard transfer method that should be used.  */
+static Lisp_Object Vw32_clipboard_type;
+static Lisp_Object QCF_TEXT, QCF_OEMTEXT, QCF_UNICODETEXT;
+
 /* Sequence number, used where possible to detect when we are pasting
    our own text.  */
 static DWORD last_clipboard_sequence_number;
@@ -110,6 +114,20 @@ DEFUN ("w32-close-clipboard", Fw32_close
 
 #endif
 
+static UINT
+get_cf_type (void)
+{
+  CHECK_SYMBOL (Vw32_clipboard_type);
+
+  if (EQ (Vw32_clipboard_type, QCF_UNICODETEXT))
+    return CF_UNICODETEXT;
+
+  if (EQ (Vw32_clipboard_type, QCF_OEMTEXT))
+    return CF_OEMTEXT;
+
+  return CF_TEXT;
+}
+
 DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data,
        Sw32_set_clipboard_data, 1, 2, 0,
        doc: /* This sets the clipboard data to the given text.  */)
@@ -134,6 +152,7 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
   src = SDATA (string);
   dst = src;
 
+#if 0 /* Disable ASCII-only optimizations */
   /* We need to know how many lines there are, since we need CRLF line
      termination for compatibility with other Windows Programs.
      avoid using strchr because it recomputes the length every time */
@@ -142,8 +161,10 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
       nlines++;
       dst++;
     }
+#endif
 
   {
+#if 0 /* Disable ASCII-only optimizations */
     /* Since we are now handling multilingual text, we must consider
        encoding text for the clipboard.  */
     int charset_info = find_charset_in_text (src, SCHARS (string),
@@ -192,6 +213,7 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
 	Vlast_coding_system_used = Qraw_text;
       }
     else
+#endif
       {
 	/* We must encode contents of OBJ to the selection coding
            system. */
@@ -242,7 +264,14 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
 							 clipboard_storage_size);
 	      }
 	    if (last_clipboard_text)
-	      memcpy (last_clipboard_text, dst, coding.produced);
+	      {
+		memcpy (last_clipboard_text, dst, coding.produced);
+		/* Add a string terminator. Set *two* bytes after the
+		   string to NUL, the second just in case we are using
+		   CF_UNICODETEXT. */
+		last_clipboard_text[coding.produced] = 
+		  last_clipboard_text[coding.produced+1] = '\0';
+	      }
 	  }
 
 	GlobalUnlock (htext);
@@ -257,7 +286,7 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
   if (!OpenClipboard ((!NILP (frame) && FRAME_W32_P (XFRAME (frame))) ? FRAME_W32_WINDOW (XFRAME (frame)) : NULL))
     goto error;
 
-  ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext);
+  ok = EmptyClipboard () && SetClipboardData (get_cf_type(), htext);
 
   CloseClipboard ();
 
@@ -277,8 +306,11 @@ DEFUN ("w32-set-clipboard-data", Fw32_se
 
   ok = FALSE;
   if (htext) GlobalFree (htext);
+
+  /* Set the first *two* bytes to NUL, the second just in case we are
+     using CF_UNICODETEXT. */
   if (last_clipboard_text)
-    *last_clipboard_text = '\0';
+    last_clipboard_text[0] = last_clipboard_text[1] = '\0';
 
   last_clipboard_sequence_number = 0;
 
@@ -296,6 +328,7 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
 {
   HANDLE htext;
   Lisp_Object ret = Qnil;
+  UINT cf_type;
 
   if (!NILP (frame))
     CHECK_LIVE_FRAME (frame);
@@ -305,7 +338,8 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
   if (!OpenClipboard ((!NILP (frame) && FRAME_W32_P (XFRAME (frame))) ? FRAME_W32_WINDOW (XFRAME (frame)) : NULL))
     goto done;
 
-  if ((htext = GetClipboardData (CF_TEXT)) == NULL)
+  cf_type = get_cf_type();
+  if ((htext = GetClipboardData (cf_type)) == NULL)
     goto closeclip;
 
   {
@@ -313,12 +347,17 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
     unsigned char *dst;
     int nbytes;
     int truelen;
+#if 0 /* Disable ASCII-only optimizations */
     int require_decoding = 0;
+#endif
 
     if ((src = (unsigned char *) GlobalLock (htext)) == NULL)
       goto closeclip;
 
-    nbytes = strlen (src);
+    if (cf_type == CF_UNICODETEXT)
+      nbytes = (lstrlenW ((WCHAR *)src) + 1) * 2;
+    else
+      nbytes = strlen (src) + 1;
 
     /* If the text in clipboard is identical to what we put there
        last time w32_set_clipboard_data was called, pretend there's no
@@ -332,6 +371,12 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
 	    && memcmp(last_clipboard_text, src, nbytes) == 0))
       goto closeclip;
 
+    /* Drop the string terminator from here on. */
+    nbytes --;
+    if (cf_type == CF_UNICODETEXT)
+      nbytes --;
+
+#if 0 /* Disable ASCII-only optimizations */
     {
       /* If the clipboard data contains any non-ascii code, we
 	 need to decode it.  */
@@ -346,8 +391,11 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
 	    }
 	}
     }
+#endif
 
+#if 0 /* Disable ASCII-only optimizations */
     if (require_decoding)
+#endif
       {
 	int bufsize;
 	unsigned char *buf;
@@ -376,6 +424,7 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
 	    && !NILP (Ffboundp (coding.post_read_conversion)))
 	  ret = run_pre_post_conversion_on_str (ret, &coding, 0);
       }
+#if 0 /* Disable ASCII-only optimizations */
     else
       {
 	/* Need to know final size after CR chars are removed because we
@@ -421,6 +470,7 @@ DEFUN ("w32-get-clipboard-data", Fw32_ge
 
 	Vlast_coding_system_used = Qraw_text;
       }
+#endif
 
     GlobalUnlock (htext);
   }
@@ -499,7 +549,21 @@ next communication only.  After the comm
 set to nil.  */);
   Vnext_selection_coding_system = Qnil;
 
-  QCLIPBOARD = intern ("CLIPBOARD");	staticpro (&QCLIPBOARD);
+  DEFVAR_LISP ("w32-clipboard-type", &Vw32_clipboard_type,
+	       doc: /* MS Windows clipboard type for the communication with other programs.
+When sending or receiving text via clipboard, this is the Windows text
+type that is used.  It can be set to `CF_TEXT', `CF_OEMTEXT' or
+`CF_UNICODETEXT'.  `CF_UNICODETEXT' is only valid on NT, Windows 2000
+or Windows XP.  `selection-coding-system' must be set to match.  The
+default value is `CF_TEXT'.  */);
+
+  QCF_TEXT	  = intern ("CF_TEXT");		staticpro (&QCF_TEXT);
+  QCF_OEMTEXT	  = intern ("CF_OEMTEXT");	staticpro (&QCF_OEMTEXT);
+  QCF_UNICODETEXT = intern ("CF_UNICODETEXT");	staticpro (&QCF_UNICODETEXT);
+  Vw32_clipboard_type = QCF_TEXT;
+
+
+  QCLIPBOARD	  = intern ("CLIPBOARD");	staticpro (&QCLIPBOARD);
 }
 
 /* arch-tag: c96e9724-5eb1-4dad-be07-289f092fd2af

[-- Attachment #3: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/emacs-devel

^ permalink raw reply	[flat|nested] 72+ messages in thread

end of thread, other threads:[~2005-02-16 17:09 UTC | newest]

Thread overview: 72+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-26 18:01 [Patch] Unicode support for the MS Windows clipboard Benjamin Riefenstahl
2004-05-27  7:58 ` Jason Rumney
2004-05-27 10:12   ` Benjamin Riefenstahl
2004-05-27 15:43     ` Jason Rumney
2004-05-27 17:46       ` Stefan Monnier
2004-05-27 21:30         ` Jason Rumney
2004-05-27  8:05 ` [Patch] " Eli Zaretskii
2004-05-27  9:45   ` Benjamin Riefenstahl
2004-05-28  7:44     ` Jason Rumney
2004-05-28  9:20     ` Eli Zaretskii
2004-05-29 14:46       ` Benjamin Riefenstahl
2004-05-28 13:26     ` Benjamin Riefenstahl
2004-05-28 14:48       ` Jason Rumney
2004-05-29  0:15         ` Kenichi Handa
2004-05-29 12:21       ` Eli Zaretskii
2004-05-29 14:52         ` Benjamin Riefenstahl
2004-05-29 17:40           ` Eli Zaretskii
2004-06-03  9:17       ` Benjamin Riefenstahl
2004-06-03 13:21         ` Kenichi Handa
2004-06-04 13:01         ` Eli Zaretskii
2004-07-26 19:17         ` Benjamin Riefenstahl
2004-07-26 19:35           ` Jason Rumney
2004-07-27 22:43             ` Benjamin Riefenstahl
2004-07-28  4:45               ` Eli Zaretskii
2004-07-28  8:02                 ` Jason Rumney
2004-07-28 18:57                   ` Eli Zaretskii
2004-07-28 11:30                 ` Benjamin Riefenstahl
2004-07-28 12:38                   ` Benjamin Riefenstahl
2004-07-28 13:03                     ` Jason Rumney
2004-07-28 13:44                       ` Benjamin Riefenstahl
2004-07-26 19:45           ` Jason Rumney
2004-07-27 11:17             ` Benjamin Riefenstahl
2004-07-27  5:07           ` Eli Zaretskii
2004-07-27 12:20             ` Benjamin Riefenstahl
2004-07-27  7:41           ` Jason Rumney
2004-07-27 11:04             ` Benjamin Riefenstahl
2004-07-27 12:24             ` Benjamin Riefenstahl
2004-07-27 13:15               ` Jason Rumney
2004-07-28  1:12               ` Tak Ota
2004-07-28 11:20                 ` Benjamin Riefenstahl
2004-07-28 11:35                   ` Jason Rumney
2004-07-28 12:08                     ` Benjamin Riefenstahl
2004-07-28 16:57                       ` Tak Ota
2004-07-28 17:34                       ` Tak Ota
2004-07-28 16:26                     ` Tak Ota
2004-07-28 18:42               ` Tak Ota
2004-07-28 21:51                 ` Tak Ota
2004-07-29 11:42                   ` Benjamin Riefenstahl
2004-07-29 16:38                     ` Tak Ota
2004-08-27 17:06               ` Tak Ota
2004-08-29 13:33                 ` Benjamin Riefenstahl
2004-08-30 20:47                   ` Unicode support for the MS Windows clipboard [new patch] Benjamin Riefenstahl
2004-08-31  4:05                     ` Eli Zaretskii
2004-09-12 19:50                       ` Benjamin Riefenstahl
2004-09-13 19:55                         ` Eli Zaretskii
2004-09-08 21:11                     ` Tak Ota
2004-09-10 13:47                     ` Kim F. Storm
2004-09-10 15:34                       ` Jason Rumney
2004-09-10 17:46                         ` Benjamin Riefenstahl
2004-09-12  9:09                           ` Richard Stallman
2004-09-12 14:11                             ` Benjamin Riefenstahl
2004-11-08 17:24                     ` Benjamin Riefenstahl
2004-11-15 21:41                       ` Tak Ota
2004-11-21 19:17                         ` Benjamin Riefenstahl
2005-02-08  0:49                       ` Tak Ota
2005-02-08  9:04                         ` Jason Rumney
2005-02-15 18:19                           ` Tak Ota
2005-02-16  9:52                             ` Jason Rumney
2005-02-16 17:09                             ` Benjamin Riefenstahl
2004-05-28 15:18     ` Unicode support for the MS Windows clipboard Stefan Monnier
2004-05-29 12:23       ` Eli Zaretskii
2004-05-27 17:48   ` [Patch] " Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).