1 /* Selection processing for Emacs on the Microsoft W32 API.
2 Copyright (C) 1993, 1994, 2004 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Written by Kevin Gallo, Benjamin Riefenstahl */
25 * Notes on usage of selection-coding-system and
26 * next-selection-coding-system on MS Windows:
28 * The selection coding system variables apply only to the version of
29 * the clipboard data that is closest in type, i.e. when a 16-bit
30 * Unicode coding system is given, they apply to he Unicode clipboard
31 * (CF_UNICODETEXT), when a well-known console codepage is given, they
32 * apply to the console version of the clipboard data (CF_OEMTEXT),
33 * else they apply to the normal 8-bit text clipboard (CF_TEXT).
35 * When pasting (getting data from the OS), the clipboard format that
36 * matches the {next-}selection-coding-system is retrieved. If
37 * Unicode is requested, but not available, 8-bit text (CF_TEXT) is
38 * used. In all other cases the OS will transparently convert
39 * formats, so no other fallback is needed.
41 * When copying or cutting (sending data to the OS), the data is
42 * announced and stored internally, but only actually rendered on
43 * request. The requester determines the format provided. The
44 * {next-}selection-coding-system is only used, when its corresponding
45 * clipboard type matches the type requested.
47 * Scenarios to use the facilities for customizing the selection
50 * ;; Generally use KOI8-R instead of the russian MS codepage for
51 * ;; the 8-bit clipboard.
52 * (set-selection-coding-system 'koi8-r-dos)
56 * ;; Create a special clipboard copy function that uses codepage
57 * ;; 1253 (Greek) to copy Greek text to a specific non-Unicode
59 * (defun greek-copy (beg end)
61 * (set-next-selection-coding-system 'cp1253-dos)
62 * (copy-region-as-kill beg end))
63 * (global-set-key "\C-c\C-c" 'greek-copy)
67 * Ideas for further directions:
69 * The encoding and decoding routines could be moved to Lisp code
70 * similar to how xselect.c does it (using well-known routine names
71 * for the delayed rendering). If the definition of which clipboard
72 * types should be supported is also moved to Lisp, functionality
73 * could be expanded to CF_HTML, CF_RTF and maybe other types.
78 #include "w32term.h" /* for all of the w32 includes */
79 #include "w32heap.h" /* os_subtype */
80 #include "blockinput.h"
81 #include "keyboard.h" /* cmd_error_internal() */
84 #include "composite.h"
87 static HGLOBAL
convert_to_handle_as_ascii (void);
88 static HGLOBAL
convert_to_handle_as_coded (Lisp_Object coding_system
);
89 static Lisp_Object
render (Lisp_Object oformat
);
90 static Lisp_Object
render_locale (void);
91 static Lisp_Object
render_all (void);
92 static void run_protected (Lisp_Object (*code
) (), Lisp_Object arg
);
93 static Lisp_Object
lisp_error_handler (Lisp_Object error
);
94 static LRESULT CALLBACK
owner_callback (HWND win
, UINT msg
,
95 WPARAM wp
, LPARAM lp
);
96 static HWND
create_owner (void);
98 static void setup_config (void);
99 static BOOL WINAPI
enum_locale_callback (/*const*/ char* loc_string
);
100 static UINT
cp_from_locale (LCID lcid
, UINT format
);
101 static Lisp_Object
coding_from_cp (UINT codepage
);
104 /* A remnant from X11: Symbol for the CLIPBORD selection type. Other
105 selections are not used on Windows, so we don't need symbols for
106 PRIMARY and SECONDARY. */
107 Lisp_Object QCLIPBOARD
;
109 /* Coding system for communicating with other programs via the
111 static Lisp_Object Vselection_coding_system
;
113 /* Coding system for the next communication with other programs. */
114 static Lisp_Object Vnext_selection_coding_system
;
116 /* Internal pseudo-constants, initialized in globals_of_w32select()
117 based on current system parameters. */
118 static LCID DEFAULT_LCID
;
119 static UINT ANSICP
, OEMCP
;
120 static Lisp_Object QUNICODE
, QANSICP
, QOEMCP
;
122 /* A hidden window just for the clipboard management. */
123 static HWND clipboard_owner
;
124 /* A flag to tell WM_DESTROYCLIPBOARD who is to blame this time (just
125 checking GetClipboardOwner() doesn't work, sadly). */
126 static int modifying_clipboard
= 0;
128 /* Configured transfer parameters, based on the last inspection of
129 selection-coding-system. */
130 static Lisp_Object cfg_coding_system
;
131 static UINT cfg_codepage
;
132 static LCID cfg_lcid
;
133 static UINT cfg_clipboard_type
;
135 /* The current state for delayed rendering. */
136 static Lisp_Object current_text
;
137 static Lisp_Object current_coding_system
;
138 static int current_requires_encoding
, current_num_nls
;
139 static UINT current_clipboard_type
;
140 static LCID current_lcid
;
143 #define ONTRACE(stmt) stmt
145 #define ONTRACE(stmt) /*stmt*/
149 /* This function assumes that there is no multibyte character in
150 current_text, so we can short-cut encoding. */
153 convert_to_handle_as_ascii (void)
155 HGLOBAL htext
= NULL
;
161 ONTRACE (fprintf (stderr
, "convert_to_handle_as_ascii\n"));
163 nbytes
= SBYTES (current_text
) + 1;
164 src
= SDATA (current_text
);
166 /* We need to add to the size the number of LF chars where we have
167 to insert CR chars (the standard CF_TEXT clipboard format uses
168 CRLF line endings, while Emacs uses just LF internally). */
170 truelen
= nbytes
+ current_num_nls
;
172 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, truelen
)) == NULL
)
175 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
181 /* convert to CRLF line endings expected by clipboard */
185 /* copy next line or remaining bytes including '\0' */
186 next
= _memccpy (dst
, src
, '\n', nbytes
);
189 /* copied one line ending with '\n' */
190 int copied
= next
- dst
;
193 /* insert '\r' before '\n' */
199 /* copied remaining partial line -> now finished */
203 GlobalUnlock (htext
);
208 /* This function assumes that there are multibyte or NUL characters in
209 current_text, or that we need to construct Unicode. It runs the
210 text through the encoding machinery. */
213 convert_to_handle_as_coded (Lisp_Object coding_system
)
215 HGLOBAL htext
= NULL
, htext2
;
218 unsigned char *dst
= NULL
;
220 struct coding_system coding
;
221 Lisp_Object string
= Qnil
;
223 ONTRACE (fprintf (stderr
, "convert_to_handle_as_coded: %s\n",
224 SDATA (SYMBOL_NAME (coding_system
))));
226 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
227 coding
.src_multibyte
= 1;
228 coding
.dst_multibyte
= 0;
229 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
230 encode_coding_iso2022 trying to dereference a null pointer. */
231 coding
.composing
= COMPOSITION_DISABLED
;
232 if (coding
.type
== coding_type_iso2022
)
233 coding
.flags
|= CODING_FLAG_ISO_SAFE
;
234 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
235 /* Force DOS line-ends. */
236 coding
.eol_type
= CODING_EOL_CRLF
;
238 if (SYMBOLP (coding
.pre_write_conversion
)
239 && !NILP (Ffboundp (coding
.pre_write_conversion
)))
240 string
= run_pre_post_conversion_on_str (current_text
, &coding
, 1);
242 string
= current_text
;
244 nbytes
= SBYTES (string
);
245 src
= SDATA (string
);
247 bufsize
= encoding_buffer_size (&coding
, nbytes
) +2;
248 htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, bufsize
);
251 dst
= (unsigned char *) GlobalLock (htext
);
255 encode_coding (&coding
, src
, dst
, nbytes
, bufsize
-2);
256 /* Add the string terminator. Add two NULs in case we are
257 producing Unicode here. */
258 dst
[coding
.produced
] = dst
[coding
.produced
+1] = '\0';
262 GlobalUnlock (htext
);
266 /* Shrink data block to actual size. */
267 htext2
= GlobalReAlloc (htext
, coding
.produced
+2,
268 GMEM_MOVEABLE
| GMEM_DDESHARE
);
269 if (htext2
!= NULL
) htext
= htext2
;
276 render (Lisp_Object oformat
)
278 HGLOBAL htext
= NULL
;
279 UINT format
= XFASTINT (oformat
);
281 ONTRACE (fprintf (stderr
, "render\n"));
283 if (NILP (current_text
))
286 if (current_requires_encoding
|| format
== CF_UNICODETEXT
)
288 if (format
== current_clipboard_type
)
289 htext
= convert_to_handle_as_coded (current_coding_system
);
294 htext
= convert_to_handle_as_coded (QUNICODE
);
300 cs
= coding_from_cp (cp_from_locale (current_lcid
, format
));
301 htext
= convert_to_handle_as_coded (cs
);
307 htext
= convert_to_handle_as_ascii ();
309 ONTRACE (fprintf (stderr
, "render: htext = 0x%08X\n", (unsigned) htext
));
314 if (SetClipboardData (format
, htext
) == NULL
)
326 HANDLE hlocale
= NULL
;
329 ONTRACE (fprintf (stderr
, "render_locale\n"));
331 if (current_lcid
== LOCALE_NEUTRAL
|| current_lcid
== DEFAULT_LCID
)
334 hlocale
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof (current_lcid
));
338 if ((lcid_ptr
= (LCID
*) GlobalLock (hlocale
)) == NULL
)
344 *lcid_ptr
= current_lcid
;
345 GlobalUnlock (hlocale
);
347 if (SetClipboardData (CF_LOCALE
, hlocale
) == NULL
)
356 /* At the end of the program, we want to ensure that our clipboard
357 data survives us. This code will do that. */
362 ONTRACE (fprintf (stderr
, "render_all\n"));
364 /* According to the docs we should not call OpenClipboard() here,
365 but testing on W2K and working code in other projects shows that
366 it is actually necessary. */
368 OpenClipboard (NULL
);
370 /* There is no usefull means to report errors here, there are none
371 expected anyway, and even if there were errors, they wouldn't do
372 any harm. So we just go ahead and do what has to be done without
373 bothering with error handling. */
375 ++modifying_clipboard
;
377 --modifying_clipboard
;
379 /* For text formats that we don't render here, the OS can use its
380 own translation rules instead, so we don't really need to offer
381 everything. To minimize memory consumption we cover three
382 possible situations based on our primary format as detected from
383 selection-coding-system (see setup_config()):
385 - Post CF_TEXT only. Let the OS convert to CF_OEMTEXT and the OS
386 (on NT) or the application (on 9x/Me) convert to
389 - Post CF_OEMTEXT only. Similar automatic conversions happen as
392 - Post CF_UNICODETEXT + CF_TEXT. 9x itself ignores
393 CF_UNICODETEXT, even though some applications can still handle
396 Note 1: We render the less capable CF_TEXT *before* the more
397 capable CF_UNICODETEXT, to prevent clobbering through automatic
398 conversions, just in case.
400 Note 2: We could check os_subtype here and only render the
401 additional CF_TEXT on 9x/Me. But OTOH with
402 current_clipboard_type == CF_UNICODETEXT we don't involve the
403 automatic conversions anywhere else, so to get consistent
404 results, we probably don't want to rely on it here either. */
408 if (current_clipboard_type
== CF_UNICODETEXT
)
409 render (make_number (CF_TEXT
));
410 render (make_number (current_clipboard_type
));
418 run_protected (Lisp_Object (*code
) (), Lisp_Object arg
)
420 /* FIXME: This works but it doesn't feel right. Too much fiddling
421 with global variables and calling strange looking functions. Is
422 this really the right way to run Lisp callbacks? */
424 extern int waiting_for_input
;
429 /* Fsignal calls abort() if it sees that waiting_for_input is
431 owfi
= waiting_for_input
;
432 waiting_for_input
= 0;
434 internal_condition_case_1 (code
, arg
, Qt
, lisp_error_handler
);
436 waiting_for_input
= owfi
;
442 lisp_error_handler (Lisp_Object error
)
444 Vsignaling_function
= Qnil
;
445 cmd_error_internal (error
, "Error in delayed clipboard rendering: ");
451 static LRESULT CALLBACK
452 owner_callback (HWND win
, UINT msg
, WPARAM wp
, LPARAM lp
)
456 case WM_RENDERFORMAT
:
457 ONTRACE (fprintf (stderr
, "WM_RENDERFORMAT\n"));
458 run_protected (render
, make_number (wp
));
461 case WM_RENDERALLFORMATS
:
462 ONTRACE (fprintf (stderr
, "WM_RENDERALLFORMATS\n"));
463 run_protected (render_all
, Qnil
);
466 case WM_DESTROYCLIPBOARD
:
467 if (!modifying_clipboard
)
469 ONTRACE (fprintf (stderr
, "WM_DESTROYCLIPBOARD (other)\n"));
471 current_coding_system
= Qnil
;
475 ONTRACE (fprintf (stderr
, "WM_DESTROYCLIPBOARD (self)\n"));
480 if (win
== clipboard_owner
)
481 clipboard_owner
= NULL
;
485 return DefWindowProc (win
, msg
, wp
, lp
);
491 static const char CLASSNAME
[] = "Emacs Clipboard";
494 memset (&wc
, 0, sizeof (wc
));
495 wc
.lpszClassName
= CLASSNAME
;
496 wc
.lpfnWndProc
= owner_callback
;
499 return CreateWindow (CLASSNAME
, CLASSNAME
, 0, 0, 0, 0, 0, NULL
, NULL
,
503 /* Called on exit by term_ntproc() in w32.c */
506 term_w32select (void)
508 /* This is needed to trigger WM_RENDERALLFORMATS. */
509 if (clipboard_owner
!= NULL
)
510 DestroyWindow (clipboard_owner
);
516 const char *coding_name
;
520 Lisp_Object new_coding_system
;
522 CHECK_SYMBOL (Vselection_coding_system
);
524 /* Check if we have it cached */
525 new_coding_system
= NILP (Vnext_selection_coding_system
) ?
526 Vselection_coding_system
: Vnext_selection_coding_system
;
527 if (!NILP (cfg_coding_system
)
528 && EQ (cfg_coding_system
, new_coding_system
))
530 cfg_coding_system
= new_coding_system
;
532 /* Set some sensible fallbacks */
533 cfg_codepage
= ANSICP
;
534 cfg_lcid
= LOCALE_NEUTRAL
;
535 cfg_clipboard_type
= CF_TEXT
;
537 /* Interpret the coding system symbol name */
538 coding_name
= SDATA (SYMBOL_NAME (cfg_coding_system
));
540 /* "(.*-)?utf-16.*" -> CF_UNICODETEXT */
541 cp
= strstr (coding_name
, "utf-16");
542 if (cp
!= NULL
&& (cp
== coding_name
|| cp
[-1] == '-'))
544 cfg_clipboard_type
= CF_UNICODETEXT
;
548 /* "cp[0-9]+.*" or "windows-[0-9]+.*" -> CF_TEXT or CF_OEMTEXT */
549 slen
= strlen (coding_name
);
550 if (slen
>= 4 && coding_name
[0] == 'c' && coding_name
[1] == 'p')
551 cp
= coding_name
+ 2;
552 else if (slen
>= 10 && memcmp (coding_name
, "windows-", 8) == 0)
553 cp
= coding_name
+ 8;
558 cfg_codepage
= strtol (cp
, &end
, 10);
560 /* Error return from strtol() or number of digits < 2 -> Restore the
561 default and drop it. */
562 if (cfg_codepage
== 0 || (end
-cp
) < 2 )
564 cfg_codepage
= ANSICP
;
568 /* Is it the currently active system default? */
569 if (cfg_codepage
== ANSICP
)
571 /* cfg_clipboard_type = CF_TEXT; */
574 if (cfg_codepage
== OEMCP
)
576 cfg_clipboard_type
= CF_OEMTEXT
;
580 /* Else determine a suitable locale the hard way. */
581 EnumSystemLocales (enum_locale_callback
, LCID_INSTALLED
);
585 enum_locale_callback (/*const*/ char* loc_string
)
590 lcid
= strtoul (loc_string
, NULL
, 16);
592 /* Is the wanted codepage the "ANSI" codepage for this locale? */
593 codepage
= cp_from_locale (lcid
, CF_TEXT
);
594 if (codepage
== cfg_codepage
)
597 cfg_clipboard_type
= CF_TEXT
;
598 return FALSE
; /* Stop enumeration */
601 /* Is the wanted codepage the OEM codepage for this locale? */
602 codepage
= cp_from_locale (lcid
, CF_OEMTEXT
);
603 if (codepage
== cfg_codepage
)
606 cfg_clipboard_type
= CF_OEMTEXT
;
607 return FALSE
; /* Stop enumeration */
610 return TRUE
; /* Continue enumeration */
614 cp_from_locale (LCID lcid
, UINT format
)
616 char buffer
[20] = "";
620 format
== CF_TEXT
? LOCALE_IDEFAULTANSICODEPAGE
: LOCALE_IDEFAULTCODEPAGE
;
622 GetLocaleInfo (lcid
, variant
, buffer
, sizeof (buffer
));
623 cp
= strtoul (buffer
, NULL
, 10);
627 else if (cp
== CP_OEMCP
)
634 coding_from_cp (UINT codepage
)
637 sprintf (buffer
, "cp%d-dos", (int) codepage
);
638 return intern (buffer
);
639 /* We don't need to check that this coding system exists right here,
640 because that is done when the coding system is actually
641 instantiated, i.e. it is passed through Fcheck_coding_system()
646 DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data
,
647 Sw32_set_clipboard_data
, 1, 2, 0,
648 doc
: /* This sets the clipboard data to the given text. */)
650 Lisp_Object string
, ignored
;
658 /* This parameter used to be the current frame, but we don't use
662 CHECK_STRING (string
);
666 current_text
= string
;
667 current_coding_system
= cfg_coding_system
;
668 current_clipboard_type
= cfg_clipboard_type
;
669 current_lcid
= cfg_lcid
;
671 current_requires_encoding
= 0;
675 /* Check for non-ASCII characters. While we are at it, count the
676 number of LFs, so we know how many CRs we will have to add later
677 (just in the case where we can use our internal ASCII rendering,
678 see code and comment in convert_to_handle_as_ascii() above). */
679 nbytes
= SBYTES (string
);
680 src
= SDATA (string
);
682 for (dst
= src
, end
= src
+nbytes
; dst
< end
; dst
++)
686 else if (*dst
>= 0x80 || *dst
== 0)
688 current_requires_encoding
= 1;
693 if (!current_requires_encoding
)
695 /* If all we have is ASCII we don't need to pretend we offer
697 current_coding_system
= Qraw_text
;
698 current_clipboard_type
= CF_TEXT
;
699 current_lcid
= LOCALE_NEUTRAL
;
702 if (!OpenClipboard (clipboard_owner
))
705 ++modifying_clipboard
;
706 ok
= EmptyClipboard ();
707 --modifying_clipboard
;
709 /* If we have something non-ASCII we may want to set a locale. We
710 do that directly (non-delayed), as it's just a small bit. */
712 ok
= !NILP(render_locale());
716 if (clipboard_owner
== NULL
)
718 /* If for some reason we don't have a clipboard_owner, we
719 just set the text format as chosen by the configuration
720 and than forget about the whole thing. */
721 ok
= !NILP(render (make_number (current_clipboard_type
)));
723 current_coding_system
= Qnil
;
727 /* Advertise all supported formats so that whatever the
728 requester chooses, only one encoding step needs to be
729 made. This is intentionally different from what we do in
730 the handler for WM_RENDERALLFORMATS. */
731 SetClipboardData (CF_UNICODETEXT
, NULL
);
732 SetClipboardData (CF_TEXT
, NULL
);
733 SetClipboardData (CF_OEMTEXT
, NULL
);
739 /* With delayed rendering we haven't really "used" this coding
740 system yet, and it's even unclear if we ever will. But this is a
741 way to tell the upper level what we *would* use under ideal
744 We don't signal the actually used coding-system later when we
745 finally render, because that can happen at any time and we don't
746 want to disturb the "foreground" action. */
748 Vlast_coding_system_used
= current_coding_system
;
750 Vnext_selection_coding_system
= Qnil
;
758 current_coding_system
= Qnil
;
763 return (ok
? string
: Qnil
);
767 DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data
,
768 Sw32_get_clipboard_data
, 0, 1, 0,
769 doc
: /* This gets the clipboard data in text format. */)
774 Lisp_Object ret
= Qnil
;
775 UINT actual_clipboard_type
;
776 int use_configured_coding_system
= 1;
778 /* This parameter used to be the current frame, but we don't use
782 /* Don't pass our own text from the clipboard (which might be
783 troublesome if the killed text includes null characters). */
784 if (!NILP (current_text
))
788 actual_clipboard_type
= cfg_clipboard_type
;
792 if (!OpenClipboard (clipboard_owner
))
795 if ((htext
= GetClipboardData (actual_clipboard_type
)) == NULL
)
797 /* If we want CF_UNICODETEXT but can't get it, the current
798 coding system is useless. OTOH we can still try and decode
799 CF_TEXT based on the locale that the system gives us and that
800 we get down below. */
801 if (actual_clipboard_type
== CF_UNICODETEXT
)
803 htext
= GetClipboardData (CF_TEXT
);
806 actual_clipboard_type
= CF_TEXT
;
807 use_configured_coding_system
= 0;
819 int require_decoding
= 0;
821 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
824 /* If the clipboard data contains any non-ascii code, we need to
825 decode it with a coding system. */
826 if (actual_clipboard_type
== CF_UNICODETEXT
)
828 nbytes
= lstrlenW ((WCHAR
*)src
) * 2;
829 require_decoding
= 1;
835 nbytes
= strlen (src
);
837 for (i
= 0; i
< nbytes
; i
++)
841 require_decoding
= 1;
847 if (require_decoding
)
849 struct coding_system coding
;
850 Lisp_Object coding_system
= Qnil
;
852 /* `next-selection-coding-system' should override everything,
853 even when the locale passed by the system disagrees. The
854 only exception is when `next-selection-coding-system'
855 requested CF_UNICODETEXT and we couldn't get that. */
856 if (use_configured_coding_system
857 && !NILP (Vnext_selection_coding_system
))
858 coding_system
= Vnext_selection_coding_system
;
860 /* If we have CF_TEXT or CF_OEMTEXT, we want to check out
862 else if (actual_clipboard_type
!= CF_UNICODETEXT
)
865 LCID lcid
= DEFAULT_LCID
;
868 /* Documentation says that the OS always generates
869 CF_LOCALE info automatically, so the locale handle
870 should always be present. Fact is that this is not
871 always true on 9x ;-(. */
872 hlocale
= GetClipboardData (CF_LOCALE
);
875 const LCID
* lcid_ptr
;
876 lcid_ptr
= (const LCID
*) GlobalLock (hlocale
);
877 if (lcid_ptr
!= NULL
)
880 GlobalUnlock (hlocale
);
883 /* 9x has garbage as the sort order (to be exact there
884 is another instance of the language id in the upper
885 word). We don't care about sort order anyway, so
886 we just filter out the unneeded mis-information to
887 avoid irritations. */
888 lcid
= MAKELCID (LANGIDFROMLCID (lcid
), SORT_DEFAULT
);
891 /* If we are using fallback from CF_UNICODETEXT, we can't
892 use the configured coding system. Also we don't want
893 to use it, if the system has supplied us with a locale
894 and it is not just the system default. */
895 if (!use_configured_coding_system
|| lcid
!= DEFAULT_LCID
)
897 cp
= cp_from_locale (lcid
, actual_clipboard_type
);
898 /* If it's just our current standard setting anyway,
899 use the coding system that the user has selected.
900 Otherwise create a new spec to match the locale
901 that was specified by the other side or the
903 if (!use_configured_coding_system
|| cp
!= cfg_codepage
)
904 coding_system
= coding_from_cp (cp
);
908 if (NILP (coding_system
))
909 coding_system
= Vselection_coding_system
;
910 Vnext_selection_coding_system
= Qnil
;
912 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
913 coding
.src_multibyte
= 0;
914 coding
.dst_multibyte
= 1;
915 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
916 /* We explicitly disable composition handling because
917 selection data should not contain any composition
919 coding
.common_flags
&= ~CODING_ANNOTATION_MASK
;
920 /* Force DOS line-ends. */
921 coding
.eol_type
= CODING_EOL_CRLF
;
923 coding
.dst_bytes
= nbytes
* 2;
924 coding
.destination
= (unsigned char *) xmalloc (coding
.dst_bytes
);
925 decode_coding_c_string (&coding
, src
, nbytes
, Qnil
);
926 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
927 ret
= make_string_from_bytes ((char *) coding
.destination
,
928 coding
.produced_char
, coding
.produced
);
929 xfree (coding
.destination
);
933 /* FIXME: We may want to repeat the code in this branch for
936 /* Need to know final size after CR chars are removed because
937 we can't change the string size manually, and doing an
938 extra copy is silly. We only remove CR when it appears as
943 /* avoid using strchr because it recomputes the length everytime */
944 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
946 if (dst
[1] == '\n') /* safe because of trailing '\0' */
951 ret
= make_uninit_string (truelen
);
953 /* Convert CRLF line endings (the standard CF_TEXT clipboard
954 format) to LF endings as used internally by Emacs. */
960 /* copy next line or remaining bytes excluding '\0' */
961 next
= _memccpy (dst
, src
, '\r', nbytes
);
964 /* copied one line ending with '\r' */
965 int copied
= next
- dst
;
970 dst
--; /* overwrite '\r' with '\n' */
973 /* copied remaining partial line -> now finished */
977 Vlast_coding_system_used
= Qraw_text
;
980 GlobalUnlock (htext
);
992 /* Support checking for a clipboard selection. */
994 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
996 doc
: /* Whether there is an owner for the given X Selection.
997 The arg should be the name of the selection in question, typically one of
998 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
999 \(Those are literal upper-case symbol names, since that's what X expects.)
1000 For convenience, the symbol nil is the same as `PRIMARY',
1001 and t is the same as `SECONDARY'. */)
1003 Lisp_Object selection
;
1005 CHECK_SYMBOL (selection
);
1007 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
1008 if the clipboard currently has valid text format contents. */
1010 if (EQ (selection
, QCLIPBOARD
))
1012 Lisp_Object val
= Qnil
;
1014 if (OpenClipboard (NULL
))
1018 while ((format
= EnumClipboardFormats (format
)))
1019 /* Check CF_TEXT in addition to cfg_clipboard_type,
1020 because we can fall back on that if CF_UNICODETEXT is
1021 not available. Actually a check for CF_TEXT only
1022 should be enough. */
1023 if (format
== cfg_clipboard_type
|| format
== CF_TEXT
)
1035 /* One-time init. Called in the un-dumped Emacs, but not in the
1039 syms_of_w32select ()
1041 defsubr (&Sw32_set_clipboard_data
);
1042 defsubr (&Sw32_get_clipboard_data
);
1043 defsubr (&Sx_selection_exists_p
);
1045 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system
,
1046 doc
: /* Coding system for communicating with other programs.
1047 When sending or receiving text via cut_buffer, selection, and
1048 clipboard, the text is encoded or decoded by this coding system.
1049 The default value is the current system default encoding on 9x/Me and
1050 `utf-16le-dos' (Unicode) on NT/W2K/XP. */);
1051 /* The actual value is set dynamically in the dumped Emacs, see
1053 Vselection_coding_system
= Qnil
;
1055 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system
,
1056 doc
: /* Coding system for the next communication with other programs.
1057 Usually, `selection-coding-system' is used for communicating with
1058 other programs. But, if this variable is set, it is used for the
1059 next communication only. After the communication, this variable is
1061 Vnext_selection_coding_system
= Qnil
;
1063 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);
1065 cfg_coding_system
= Qnil
; staticpro (&cfg_coding_system
);
1066 current_text
= Qnil
; staticpro (¤t_text
);
1067 current_coding_system
= Qnil
; staticpro (¤t_coding_system
);
1069 QUNICODE
= intern ("utf-16le-dos"); staticpro (&QUNICODE
);
1070 QANSICP
= Qnil
; staticpro (&QANSICP
);
1071 QOEMCP
= Qnil
; staticpro (&QOEMCP
);
1074 /* One-time init. Called in the dumped Emacs, but not in the
1075 un-dumped version. */
1078 globals_of_w32select ()
1080 DEFAULT_LCID
= GetUserDefaultLCID ();
1081 /* Drop the sort order from the LCID, so we can compare this with
1082 CF_LOCALE objects that have the same fix on 9x. */
1083 DEFAULT_LCID
= MAKELCID (LANGIDFROMLCID (DEFAULT_LCID
), SORT_DEFAULT
);
1086 OEMCP
= GetOEMCP ();
1088 QANSICP
= coding_from_cp (ANSICP
);
1089 QOEMCP
= coding_from_cp (OEMCP
);
1091 if (os_subtype
== OS_NT
)
1092 Vselection_coding_system
= QUNICODE
;
1093 else if (inhibit_window_system
)
1094 Vselection_coding_system
= QOEMCP
;
1096 Vselection_coding_system
= QANSICP
;
1098 clipboard_owner
= create_owner ();
1101 /* arch-tag: c96e9724-5eb1-4dad-be07-289f092fd2af
1102 (do not change this comment) */