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