1 /* Selection processing for Emacs on the Microsoft Windows API.
3 Copyright (C) 1993-1994, 2001-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Kevin Gallo, Benjamin Riefenstahl */
24 * Notes on usage of selection-coding-system and
25 * next-selection-coding-system on MS Windows:
27 * The selection coding system variables apply only to the version of
28 * the clipboard data that is closest in type, i.e. when a 16-bit
29 * Unicode coding system is given, they apply to he Unicode clipboard
30 * (CF_UNICODETEXT), when a well-known console codepage is given, they
31 * apply to the console version of the clipboard data (CF_OEMTEXT),
32 * else they apply to the normal 8-bit text clipboard (CF_TEXT).
34 * When pasting (getting data from the OS), the clipboard format that
35 * matches the {next-}selection-coding-system is retrieved. If
36 * Unicode is requested, but not available, 8-bit text (CF_TEXT) is
37 * used. In all other cases the OS will transparently convert
38 * formats, so no other fallback is needed.
40 * When copying or cutting (sending data to the OS), the data is
41 * announced and stored internally, but only actually rendered on
42 * request. The requestor determines the format provided. The
43 * {next-}selection-coding-system is only used, when its corresponding
44 * clipboard type matches the type requested.
46 * Scenarios to use the facilities for customizing the selection
49 * ;; Generally use KOI8-R instead of the russian MS codepage for
50 * ;; the 8-bit clipboard.
51 * (set-selection-coding-system 'koi8-r-dos)
55 * ;; Create a special clipboard copy function that uses codepage
56 * ;; 1253 (Greek) to copy Greek text to a specific non-Unicode
58 * (defun greek-copy (beg end)
60 * (set-next-selection-coding-system 'cp1253-dos)
61 * (copy-region-as-kill beg end))
62 * (global-set-key "\C-c\C-c" 'greek-copy)
66 * Ideas for further directions:
68 * The encoding and decoding routines could be moved to Lisp code
69 * similar to how xselect.c does it (using well-known routine names
70 * for the delayed rendering). If the definition of which clipboard
71 * types should be supported is also moved to Lisp, functionality
72 * could be expanded to CF_HTML, CF_RTF and maybe other types.
77 #include "w32common.h" /* os_subtype */
78 #include "w32term.h" /* for all of the w32 includes */
80 #include "blockinput.h"
83 #include "composite.h"
88 #define _memccpy memccpy
91 static HGLOBAL
convert_to_handle_as_ascii (void);
92 static HGLOBAL
convert_to_handle_as_coded (Lisp_Object coding_system
);
93 static Lisp_Object
render (Lisp_Object oformat
);
94 static Lisp_Object
render_locale (void);
95 static Lisp_Object
render_all (Lisp_Object ignore
);
96 static void run_protected (Lisp_Object (*code
) (Lisp_Object
), Lisp_Object arg
);
97 static Lisp_Object
lisp_error_handler (Lisp_Object error
);
98 static LRESULT CALLBACK
owner_callback (HWND win
, UINT msg
,
99 WPARAM wp
, LPARAM lp
);
100 static HWND
create_owner (void);
102 static void setup_config (void);
103 static BOOL WINAPI
enum_locale_callback (/*const*/ char* loc_string
);
104 static UINT
cp_from_locale (LCID lcid
, UINT format
);
105 static Lisp_Object
coding_from_cp (UINT codepage
);
106 static Lisp_Object
validate_coding_system (Lisp_Object coding_system
);
107 static void setup_windows_coding_system (Lisp_Object coding_system
,
108 struct coding_system
* coding
);
111 /* A remnant from X11: Symbol for the CLIPBORD selection type. Other
112 selections are not used on Windows, so we don't need symbols for
113 PRIMARY and SECONDARY. */
114 Lisp_Object QCLIPBOARD
;
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
)
216 unsigned char *dst
= NULL
;
217 struct coding_system coding
;
219 ONTRACE (fprintf (stderr
, "convert_to_handle_as_coded: %s\n",
220 SDATA (SYMBOL_NAME (coding_system
))));
222 setup_windows_coding_system (coding_system
, &coding
);
223 coding
.dst_bytes
= SBYTES (current_text
) * 2;
224 coding
.destination
= xmalloc (coding
.dst_bytes
);
225 encode_coding_object (&coding
, current_text
, 0, 0,
226 SCHARS (current_text
), SBYTES (current_text
), Qnil
);
228 htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, coding
.produced
+2);
231 dst
= (unsigned char *) GlobalLock (htext
);
235 memcpy (dst
, coding
.destination
, coding
.produced
);
236 /* Add the string terminator. Add two NULs in case we are
237 producing Unicode here. */
238 dst
[coding
.produced
] = dst
[coding
.produced
+1] = '\0';
240 GlobalUnlock (htext
);
243 xfree (coding
.destination
);
249 render (Lisp_Object oformat
)
251 HGLOBAL htext
= NULL
;
252 UINT format
= XFASTINT (oformat
);
254 ONTRACE (fprintf (stderr
, "render\n"));
256 if (NILP (current_text
))
259 if (current_requires_encoding
|| format
== CF_UNICODETEXT
)
261 if (format
== current_clipboard_type
)
262 htext
= convert_to_handle_as_coded (current_coding_system
);
267 htext
= convert_to_handle_as_coded (QUNICODE
);
273 cs
= coding_from_cp (cp_from_locale (current_lcid
, format
));
274 htext
= convert_to_handle_as_coded (cs
);
280 htext
= convert_to_handle_as_ascii ();
282 ONTRACE (fprintf (stderr
, "render: htext = 0x%08X\n", (unsigned) htext
));
287 if (SetClipboardData (format
, htext
) == NULL
)
299 HANDLE hlocale
= NULL
;
302 ONTRACE (fprintf (stderr
, "render_locale\n"));
304 if (current_lcid
== LOCALE_NEUTRAL
|| current_lcid
== DEFAULT_LCID
)
307 hlocale
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof (current_lcid
));
311 if ((lcid_ptr
= (LCID
*) GlobalLock (hlocale
)) == NULL
)
313 GlobalFree (hlocale
);
317 *lcid_ptr
= current_lcid
;
318 GlobalUnlock (hlocale
);
320 if (SetClipboardData (CF_LOCALE
, hlocale
) == NULL
)
322 GlobalFree (hlocale
);
329 /* At the end of the program, we want to ensure that our clipboard
330 data survives us. This code will do that. */
333 render_all (Lisp_Object ignore
)
335 ONTRACE (fprintf (stderr
, "render_all\n"));
337 /* According to the docs we should not call OpenClipboard() here,
338 but testing on W2K and working code in other projects shows that
339 it is actually necessary. */
341 OpenClipboard (NULL
);
343 /* There is no useful means to report errors here, there are none
344 expected anyway, and even if there were errors, they wouldn't do
345 any harm. So we just go ahead and do what has to be done without
346 bothering with error handling. */
348 ++modifying_clipboard
;
350 --modifying_clipboard
;
352 /* For text formats that we don't render here, the OS can use its
353 own translation rules instead, so we don't really need to offer
354 everything. To minimize memory consumption we cover three
355 possible situations based on our primary format as detected from
356 selection-coding-system (see setup_config()):
358 - Post CF_TEXT only. Let the OS convert to CF_OEMTEXT and the OS
359 (on NT) or the application (on 9x/Me) convert to
362 - Post CF_OEMTEXT only. Similar automatic conversions happen as
365 - Post CF_UNICODETEXT + CF_TEXT. 9x itself ignores
366 CF_UNICODETEXT, even though some applications can still handle
369 Note 1: We render the less capable CF_TEXT *before* the more
370 capable CF_UNICODETEXT, to prevent clobbering through automatic
371 conversions, just in case.
373 Note 2: We could check os_subtype here and only render the
374 additional CF_TEXT on 9x/Me. But OTOH with
375 current_clipboard_type == CF_UNICODETEXT we don't involve the
376 automatic conversions anywhere else, so to get consistent
377 results, we probably don't want to rely on it here either. */
381 if (current_clipboard_type
== CF_UNICODETEXT
)
382 render (make_number (CF_TEXT
));
383 render (make_number (current_clipboard_type
));
391 run_protected (Lisp_Object (*code
) (Lisp_Object
), Lisp_Object arg
)
393 /* FIXME: This works but it doesn't feel right. Too much fiddling
394 with global variables and calling strange looking functions. Is
395 this really the right way to run Lisp callbacks? */
401 /* Fsignal calls emacs_abort () if it sees that waiting_for_input is
403 owfi
= waiting_for_input
;
404 waiting_for_input
= 0;
406 internal_condition_case_1 (code
, arg
, Qt
, lisp_error_handler
);
408 waiting_for_input
= owfi
;
414 lisp_error_handler (Lisp_Object error
)
416 Vsignaling_function
= Qnil
;
417 cmd_error_internal (error
, "Error in delayed clipboard rendering: ");
423 static LRESULT CALLBACK
424 owner_callback (HWND win
, UINT msg
, WPARAM wp
, LPARAM lp
)
428 case WM_RENDERFORMAT
:
429 ONTRACE (fprintf (stderr
, "WM_RENDERFORMAT\n"));
430 run_protected (render
, make_number (wp
));
433 case WM_RENDERALLFORMATS
:
434 ONTRACE (fprintf (stderr
, "WM_RENDERALLFORMATS\n"));
435 run_protected (render_all
, Qnil
);
438 case WM_DESTROYCLIPBOARD
:
439 if (!modifying_clipboard
)
441 ONTRACE (fprintf (stderr
, "WM_DESTROYCLIPBOARD (other)\n"));
443 current_coding_system
= Qnil
;
447 ONTRACE (fprintf (stderr
, "WM_DESTROYCLIPBOARD (self)\n"));
452 if (win
== clipboard_owner
)
453 clipboard_owner
= NULL
;
457 return DefWindowProc (win
, msg
, wp
, lp
);
463 static const char CLASSNAME
[] = "Emacs Clipboard";
466 memset (&wc
, 0, sizeof (wc
));
467 wc
.lpszClassName
= CLASSNAME
;
468 wc
.lpfnWndProc
= owner_callback
;
471 return CreateWindow (CLASSNAME
, CLASSNAME
, 0, 0, 0, 0, 0, NULL
, NULL
,
475 /* Called on exit by term_ntproc() in w32.c */
478 term_w32select (void)
480 /* This is needed to trigger WM_RENDERALLFORMATS. */
481 if (clipboard_owner
!= NULL
)
483 DestroyWindow (clipboard_owner
);
484 clipboard_owner
= NULL
;
491 const char *coding_name
;
495 Lisp_Object coding_system
;
496 Lisp_Object dos_coding_system
;
498 CHECK_SYMBOL (Vselection_coding_system
);
500 coding_system
= NILP (Vnext_selection_coding_system
) ?
501 Vselection_coding_system
: Vnext_selection_coding_system
;
503 dos_coding_system
= validate_coding_system (coding_system
);
504 if (NILP (dos_coding_system
))
506 list2 (build_string ("Coding system is invalid or doesn't have "
507 "an eol variant for dos line ends"),
510 /* Check if we have it cached */
511 if (!NILP (cfg_coding_system
)
512 && EQ (cfg_coding_system
, dos_coding_system
))
514 cfg_coding_system
= dos_coding_system
;
516 /* Set some sensible fallbacks */
517 cfg_codepage
= ANSICP
;
518 cfg_lcid
= LOCALE_NEUTRAL
;
519 cfg_clipboard_type
= CF_TEXT
;
521 /* Interpret the coding system symbol name */
522 coding_name
= SDATA (SYMBOL_NAME (cfg_coding_system
));
524 /* "(.*-)?utf-16.*" -> CF_UNICODETEXT */
525 cp
= strstr (coding_name
, "utf-16");
526 if (cp
!= NULL
&& (cp
== coding_name
|| cp
[-1] == '-'))
528 cfg_clipboard_type
= CF_UNICODETEXT
;
532 /* "cp[0-9]+.*" or "windows-[0-9]+.*" -> CF_TEXT or CF_OEMTEXT */
533 slen
= strlen (coding_name
);
534 if (slen
>= 4 && coding_name
[0] == 'c' && coding_name
[1] == 'p')
535 cp
= coding_name
+ 2;
536 else if (slen
>= 10 && memcmp (coding_name
, "windows-", 8) == 0)
537 cp
= coding_name
+ 8;
542 cfg_codepage
= strtol (cp
, &end
, 10);
544 /* Error return from strtol() or number of digits < 2 -> Restore the
545 default and drop it. */
546 if (cfg_codepage
== 0 || (end
-cp
) < 2 )
548 cfg_codepage
= ANSICP
;
552 /* Is it the currently active system default? */
553 if (cfg_codepage
== ANSICP
)
555 /* cfg_clipboard_type = CF_TEXT; */
558 if (cfg_codepage
== OEMCP
)
560 cfg_clipboard_type
= CF_OEMTEXT
;
564 /* Else determine a suitable locale the hard way. */
565 EnumSystemLocales (enum_locale_callback
, LCID_INSTALLED
);
569 enum_locale_callback (/*const*/ char* loc_string
)
574 lcid
= strtoul (loc_string
, NULL
, 16);
576 /* Is the wanted codepage the "ANSI" codepage for this locale? */
577 codepage
= cp_from_locale (lcid
, CF_TEXT
);
578 if (codepage
== cfg_codepage
)
581 cfg_clipboard_type
= CF_TEXT
;
582 return FALSE
; /* Stop enumeration */
585 /* Is the wanted codepage the OEM codepage for this locale? */
586 codepage
= cp_from_locale (lcid
, CF_OEMTEXT
);
587 if (codepage
== cfg_codepage
)
590 cfg_clipboard_type
= CF_OEMTEXT
;
591 return FALSE
; /* Stop enumeration */
594 return TRUE
; /* Continue enumeration */
598 cp_from_locale (LCID lcid
, UINT format
)
600 char buffer
[20] = "";
604 format
== CF_TEXT
? LOCALE_IDEFAULTANSICODEPAGE
: LOCALE_IDEFAULTCODEPAGE
;
606 GetLocaleInfo (lcid
, variant
, buffer
, sizeof (buffer
));
607 cp
= strtoul (buffer
, NULL
, 10);
611 else if (cp
== CP_OEMCP
)
618 coding_from_cp (UINT codepage
)
621 sprintf (buffer
, "cp%d-dos", (int) codepage
);
622 return intern (buffer
);
623 /* We don't need to check that this coding system actually exists
624 right here, because that is done later for all coding systems
625 used, regardless of where they originate. */
629 validate_coding_system (Lisp_Object coding_system
)
631 Lisp_Object eol_type
;
633 /* Make sure the input is valid. */
634 if (NILP (Fcoding_system_p (coding_system
)))
637 /* Make sure we use a DOS coding system as mandated by the system
639 eol_type
= Fcoding_system_eol_type (coding_system
);
641 /* Already a DOS coding system? */
642 if (EQ (eol_type
, make_number (1)))
643 return coding_system
;
645 /* Get EOL_TYPE vector of the base of CODING_SYSTEM. */
646 if (!VECTORP (eol_type
))
648 eol_type
= Fcoding_system_eol_type (Fcoding_system_base (coding_system
));
649 if (!VECTORP (eol_type
))
653 return AREF (eol_type
, 1);
657 setup_windows_coding_system (Lisp_Object coding_system
,
658 struct coding_system
* coding
)
660 memset (coding
, 0, sizeof (*coding
));
661 setup_coding_system (coding_system
, coding
);
663 /* Unset CODING_ANNOTATE_COMPOSITION_MASK. Previous code had
664 comments about crashes in encode_coding_iso2022 trying to
665 dereference a null pointer when composition was on. Selection
666 data should not contain any composition sequence on Windows.
668 CODING_ANNOTATION_MASK also includes
669 CODING_ANNOTATE_DIRECTION_MASK and CODING_ANNOTATE_CHARSET_MASK,
670 which both apply to ISO6429 only. We don't know if these really
671 need to be unset on Windows, but it probably doesn't hurt
673 coding
->mode
&= ~CODING_ANNOTATION_MASK
;
674 coding
->mode
|= CODING_MODE_LAST_BLOCK
| CODING_MODE_SAFE_ENCODING
;
679 DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data
,
680 Sw32_set_clipboard_data
, 1, 2, 0,
681 doc
: /* This sets the clipboard data to the given text. */)
682 (Lisp_Object string
, Lisp_Object ignored
)
690 /* This parameter used to be the current frame, but we don't use
694 CHECK_STRING (string
);
698 current_text
= string
;
699 current_coding_system
= cfg_coding_system
;
700 current_clipboard_type
= cfg_clipboard_type
;
701 current_lcid
= cfg_lcid
;
703 current_requires_encoding
= 0;
707 /* Check for non-ASCII characters. While we are at it, count the
708 number of LFs, so we know how many CRs we will have to add later
709 (just in the case where we can use our internal ASCII rendering,
710 see code and comment in convert_to_handle_as_ascii() above). */
711 nbytes
= SBYTES (string
);
712 src
= SDATA (string
);
714 for (dst
= src
, end
= src
+nbytes
; dst
< end
; dst
++)
718 else if (*dst
>= 0x80 || *dst
== 0)
720 current_requires_encoding
= 1;
725 if (!current_requires_encoding
)
727 /* If all we have is ASCII we don't need to pretend we offer
729 current_coding_system
= Qraw_text
;
730 current_clipboard_type
= CF_TEXT
;
731 current_lcid
= LOCALE_NEUTRAL
;
734 if (!OpenClipboard (clipboard_owner
))
737 ++modifying_clipboard
;
738 ok
= EmptyClipboard ();
739 --modifying_clipboard
;
741 /* If we have something non-ASCII we may want to set a locale. We
742 do that directly (non-delayed), as it's just a small bit. */
744 ok
= !NILP (render_locale ());
748 if (clipboard_owner
== NULL
)
750 /* If for some reason we don't have a clipboard_owner, we
751 just set the text format as chosen by the configuration
752 and than forget about the whole thing. */
753 ok
= !NILP (render (make_number (current_clipboard_type
)));
755 current_coding_system
= Qnil
;
759 /* Advertise all supported formats so that whatever the
760 requestor chooses, only one encoding step needs to be
761 made. This is intentionally different from what we do in
762 the handler for WM_RENDERALLFORMATS. */
763 SetClipboardData (CF_UNICODETEXT
, NULL
);
764 SetClipboardData (CF_TEXT
, NULL
);
765 SetClipboardData (CF_OEMTEXT
, NULL
);
771 /* With delayed rendering we haven't really "used" this coding
772 system yet, and it's even unclear if we ever will. But this is a
773 way to tell the upper level what we *would* use under ideal
776 We don't signal the actually used coding-system later when we
777 finally render, because that can happen at any time and we don't
778 want to disturb the "foreground" action. */
780 Vlast_coding_system_used
= current_coding_system
;
782 Vnext_selection_coding_system
= Qnil
;
790 current_coding_system
= Qnil
;
795 return (ok
? string
: Qnil
);
799 DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data
,
800 Sw32_get_clipboard_data
, 0, 1, 0,
801 doc
: /* This gets the clipboard data in text format. */)
802 (Lisp_Object ignored
)
805 Lisp_Object ret
= Qnil
;
806 UINT actual_clipboard_type
;
807 int use_configured_coding_system
= 1;
809 /* This parameter used to be the current frame, but we don't use
813 /* Don't pass our own text from the clipboard (which might be
814 troublesome if the killed text includes null characters). */
815 if (!NILP (current_text
))
819 actual_clipboard_type
= cfg_clipboard_type
;
823 if (!OpenClipboard (clipboard_owner
))
826 if ((htext
= GetClipboardData (actual_clipboard_type
)) == NULL
)
828 /* If we want CF_UNICODETEXT but can't get it, the current
829 coding system is useless. OTOH we can still try and decode
830 CF_TEXT based on the locale that the system gives us and that
831 we get down below. */
832 if (actual_clipboard_type
== CF_UNICODETEXT
)
834 htext
= GetClipboardData (CF_TEXT
);
837 actual_clipboard_type
= CF_TEXT
;
838 use_configured_coding_system
= 0;
850 int require_decoding
= 0;
852 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
855 /* If the clipboard data contains any non-ascii code, we need to
856 decode it with a coding system. */
857 if (actual_clipboard_type
== CF_UNICODETEXT
)
859 nbytes
= lstrlenW ((WCHAR
*)src
) * 2;
860 require_decoding
= 1;
866 nbytes
= strlen (src
);
868 for (i
= 0; i
< nbytes
; i
++)
872 require_decoding
= 1;
878 if (require_decoding
)
880 struct coding_system coding
;
881 Lisp_Object coding_system
= Qnil
;
882 Lisp_Object dos_coding_system
;
884 /* `next-selection-coding-system' should override everything,
885 even when the locale passed by the system disagrees. The
886 only exception is when `next-selection-coding-system'
887 requested CF_UNICODETEXT and we couldn't get that. */
888 if (use_configured_coding_system
889 && !NILP (Vnext_selection_coding_system
))
890 coding_system
= Vnext_selection_coding_system
;
892 /* If we have CF_TEXT or CF_OEMTEXT, we want to check out
894 else if (actual_clipboard_type
!= CF_UNICODETEXT
)
897 LCID lcid
= DEFAULT_LCID
;
900 /* Documentation says that the OS always generates
901 CF_LOCALE info automatically, so the locale handle
902 should always be present. Fact is that this is not
903 always true on 9x ;-(. */
904 hlocale
= GetClipboardData (CF_LOCALE
);
907 const LCID
* lcid_ptr
;
908 lcid_ptr
= (const LCID
*) GlobalLock (hlocale
);
909 if (lcid_ptr
!= NULL
)
912 GlobalUnlock (hlocale
);
915 /* 9x has garbage as the sort order (to be exact there
916 is another instance of the language id in the upper
917 word). We don't care about sort order anyway, so
918 we just filter out the unneeded mis-information to
919 avoid irritations. */
920 lcid
= MAKELCID (LANGIDFROMLCID (lcid
), SORT_DEFAULT
);
923 /* If we are using fallback from CF_UNICODETEXT, we can't
924 use the configured coding system. Also we don't want
925 to use it, if the system has supplied us with a locale
926 and it is not just the system default. */
927 if (!use_configured_coding_system
|| lcid
!= DEFAULT_LCID
)
929 cp
= cp_from_locale (lcid
, actual_clipboard_type
);
930 /* If it's just our current standard setting anyway,
931 use the coding system that the user has selected.
932 Otherwise create a new spec to match the locale
933 that was specified by the other side or the
935 if (!use_configured_coding_system
|| cp
!= cfg_codepage
)
936 coding_system
= coding_from_cp (cp
);
940 if (NILP (coding_system
))
941 coding_system
= Vselection_coding_system
;
942 Vnext_selection_coding_system
= Qnil
;
944 dos_coding_system
= validate_coding_system (coding_system
);
945 if (!NILP (dos_coding_system
))
947 setup_windows_coding_system (dos_coding_system
, &coding
);
949 decode_coding_object (&coding
, Qnil
, 0, 0, nbytes
, nbytes
, Qt
);
950 ret
= coding
.dst_object
;
952 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
957 /* FIXME: We may want to repeat the code in this branch for
960 /* Need to know final size after CR chars are removed because
961 we can't change the string size manually, and doing an
962 extra copy is silly. We only remove CR when it appears as
967 /* avoid using strchr because it recomputes the length everytime */
968 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
970 if (dst
[1] == '\n') /* safe because of trailing '\0' */
975 ret
= make_uninit_string (truelen
);
977 /* Convert CRLF line endings (the standard CF_TEXT clipboard
978 format) to LF endings as used internally by Emacs. */
984 /* copy next line or remaining bytes excluding '\0' */
985 next
= _memccpy (dst
, src
, '\r', nbytes
);
988 /* copied one line ending with '\r' */
989 int copied
= next
- dst
;
994 dst
--; /* overwrite '\r' with '\n' */
997 /* copied remaining partial line -> now finished */
1001 Vlast_coding_system_used
= Qraw_text
;
1004 GlobalUnlock (htext
);
1016 /* Support checking for a clipboard selection. */
1018 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
1020 doc
: /* Whether there is an owner for the given X selection.
1021 SELECTION should be the name of the selection in question, typically
1022 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
1023 these literal upper-case names.) The symbol nil is the same as
1024 `PRIMARY', and t is the same as `SECONDARY'.
1026 TERMINAL should be a terminal object or a frame specifying the X
1027 server to query. If omitted or nil, that stands for the selected
1028 frame's display, or the first available X display. */)
1029 (Lisp_Object selection
, Lisp_Object terminal
)
1031 CHECK_SYMBOL (selection
);
1033 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
1034 if the clipboard currently has valid text format contents. */
1036 if (EQ (selection
, QCLIPBOARD
))
1038 Lisp_Object val
= Qnil
;
1042 if (OpenClipboard (NULL
))
1045 while ((format
= EnumClipboardFormats (format
)))
1046 /* Check CF_TEXT in addition to cfg_clipboard_type,
1047 because we can fall back on that if CF_UNICODETEXT is
1048 not available. Actually a check for CF_TEXT only
1049 should be enough. */
1050 if (format
== cfg_clipboard_type
|| format
== CF_TEXT
)
1062 /* One-time init. Called in the un-dumped Emacs, but not in the
1066 syms_of_w32select (void)
1068 defsubr (&Sw32_set_clipboard_data
);
1069 defsubr (&Sw32_get_clipboard_data
);
1070 defsubr (&Sx_selection_exists_p
);
1072 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system
,
1073 doc
: /* Coding system for communicating with other programs.
1075 For MS-Windows and MS-DOS:
1076 When sending or receiving text via selection and clipboard, the text
1077 is encoded or decoded by this coding system. The default value is
1078 the current system default encoding on 9x/Me, `utf-16le-dos'
1079 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
1082 When sending text via selection and clipboard, if the target
1083 data-type matches with the type of this coding system, it is used
1084 for encoding the text. Otherwise (including the case that this
1085 variable is nil), a proper coding system is used as below:
1087 data-type coding system
1088 --------- -------------
1090 COMPOUND_TEXT compound-text-with-extensions
1092 C_STRING no-conversion
1094 When receiving text, if this coding system is non-nil, it is used
1095 for decoding regardless of the data-type. If this is nil, a
1096 proper coding system is used according to the data-type as above.
1098 See also the documentation of the variable `x-select-request-type' how
1099 to control which data-type to request for receiving text.
1101 The default value is nil. */);
1102 /* The actual value is set dynamically in the dumped Emacs, see
1104 Vselection_coding_system
= Qnil
;
1106 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system
,
1107 doc
: /* Coding system for the next communication with other programs.
1108 Usually, `selection-coding-system' is used for communicating with
1109 other programs (X Windows clients or MS Windows programs). But, if this
1110 variable is set, it is used for the next communication only.
1111 After the communication, this variable is set to nil. */);
1112 Vnext_selection_coding_system
= Qnil
;
1114 DEFSYM (QCLIPBOARD
, "CLIPBOARD");
1116 cfg_coding_system
= Qnil
; staticpro (&cfg_coding_system
);
1117 current_text
= Qnil
; staticpro (¤t_text
);
1118 current_coding_system
= Qnil
; staticpro (¤t_coding_system
);
1120 DEFSYM (QUNICODE
, "utf-16le-dos");
1121 QANSICP
= Qnil
; staticpro (&QANSICP
);
1122 QOEMCP
= Qnil
; staticpro (&QOEMCP
);
1125 /* One-time init. Called in the dumped Emacs, but not in the
1126 un-dumped version. */
1129 globals_of_w32select (void)
1131 DEFAULT_LCID
= GetUserDefaultLCID ();
1132 /* Drop the sort order from the LCID, so we can compare this with
1133 CF_LOCALE objects that have the same fix on 9x. */
1134 DEFAULT_LCID
= MAKELCID (LANGIDFROMLCID (DEFAULT_LCID
), SORT_DEFAULT
);
1137 OEMCP
= GetOEMCP ();
1139 QANSICP
= coding_from_cp (ANSICP
);
1140 QOEMCP
= coding_from_cp (OEMCP
);
1142 if (os_subtype
== OS_NT
)
1143 Vselection_coding_system
= QUNICODE
;
1144 else if (inhibit_window_system
)
1145 Vselection_coding_system
= QOEMCP
;
1147 Vselection_coding_system
= QANSICP
;
1149 clipboard_owner
= create_owner ();