Commit | Line | Data |
---|---|---|
e9e23e23 | 1 | /* Selection processing for Emacs on the Microsoft W32 API. |
e9bffc61 | 2 | |
73b0cd50 | 3 | Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc. |
41a48e45 | 4 | |
3b7ad313 EN |
5 | This file is part of GNU Emacs. |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
3b7ad313 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
3b7ad313 EN |
11 | |
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
ee78dc32 | 19 | |
52c7f9ee JR |
20 | /* Written by Kevin Gallo, Benjamin Riefenstahl */ |
21 | ||
22 | ||
23 | /* | |
24 | * Notes on usage of selection-coding-system and | |
25 | * next-selection-coding-system on MS Windows: | |
26 | * | |
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). | |
b56ceb92 | 33 | * |
52c7f9ee JR |
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. | |
39 | * | |
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 requester determines the format provided. The | |
43 | * {next-}selection-coding-system is only used, when its corresponding | |
44 | * clipboard type matches the type requested. | |
45 | * | |
46 | * Scenarios to use the facilities for customizing the selection | |
47 | * coding system are: | |
b56ceb92 | 48 | * |
52c7f9ee JR |
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) | |
b56ceb92 | 52 | * |
52c7f9ee | 53 | * Or |
b56ceb92 | 54 | * |
52c7f9ee JR |
55 | * ;; Create a special clipboard copy function that uses codepage |
56 | * ;; 1253 (Greek) to copy Greek text to a specific non-Unicode | |
57 | * ;; application. | |
58 | * (defun greek-copy (beg end) | |
59 | * (interactive "r") | |
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) | |
63 | */ | |
64 | ||
65 | /* | |
66 | * Ideas for further directions: | |
67 | * | |
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. | |
73 | */ | |
b56ceb92 | 74 | |
ee78dc32 | 75 | #include <config.h> |
d7306fe6 | 76 | #include <setjmp.h> |
ee78dc32 | 77 | #include "lisp.h" |
fbd6baed | 78 | #include "w32term.h" /* for all of the w32 includes */ |
52c7f9ee | 79 | #include "w32heap.h" /* os_subtype */ |
ee78dc32 | 80 | #include "blockinput.h" |
52c7f9ee | 81 | #include "keyboard.h" /* cmd_error_internal() */ |
bbb059f3 AI |
82 | #include "charset.h" |
83 | #include "coding.h" | |
c949d9d0 | 84 | #include "character.h" |
f7d05dc4 | 85 | #include "composite.h" |
ee78dc32 | 86 | |
52c7f9ee JR |
87 | |
88 | static HGLOBAL convert_to_handle_as_ascii (void); | |
89 | static HGLOBAL convert_to_handle_as_coded (Lisp_Object coding_system); | |
90 | static Lisp_Object render (Lisp_Object oformat); | |
91 | static Lisp_Object render_locale (void); | |
b56ceb92 JB |
92 | static Lisp_Object render_all (Lisp_Object ignore); |
93 | static void run_protected (Lisp_Object (*code) (Lisp_Object), Lisp_Object arg); | |
52c7f9ee JR |
94 | static Lisp_Object lisp_error_handler (Lisp_Object error); |
95 | static LRESULT CALLBACK owner_callback (HWND win, UINT msg, | |
96 | WPARAM wp, LPARAM lp); | |
97 | static HWND create_owner (void); | |
98 | ||
99 | static void setup_config (void); | |
100 | static BOOL WINAPI enum_locale_callback (/*const*/ char* loc_string); | |
101 | static UINT cp_from_locale (LCID lcid, UINT format); | |
102 | static Lisp_Object coding_from_cp (UINT codepage); | |
9a38f8d5 KH |
103 | static Lisp_Object validate_coding_system (Lisp_Object coding_system); |
104 | static void setup_windows_coding_system (Lisp_Object coding_system, | |
105 | struct coding_system * coding); | |
52c7f9ee JR |
106 | |
107 | ||
108 | /* A remnant from X11: Symbol for the CLIPBORD selection type. Other | |
109 | selections are not used on Windows, so we don't need symbols for | |
110 | PRIMARY and SECONDARY. */ | |
9aa94bd5 KH |
111 | Lisp_Object QCLIPBOARD; |
112 | ||
52c7f9ee JR |
113 | /* Internal pseudo-constants, initialized in globals_of_w32select() |
114 | based on current system parameters. */ | |
115 | static LCID DEFAULT_LCID; | |
116 | static UINT ANSICP, OEMCP; | |
117 | static Lisp_Object QUNICODE, QANSICP, QOEMCP; | |
118 | ||
119 | /* A hidden window just for the clipboard management. */ | |
120 | static HWND clipboard_owner; | |
121 | /* A flag to tell WM_DESTROYCLIPBOARD who is to blame this time (just | |
122 | checking GetClipboardOwner() doesn't work, sadly). */ | |
123 | static int modifying_clipboard = 0; | |
124 | ||
125 | /* Configured transfer parameters, based on the last inspection of | |
126 | selection-coding-system. */ | |
127 | static Lisp_Object cfg_coding_system; | |
128 | static UINT cfg_codepage; | |
129 | static LCID cfg_lcid; | |
130 | static UINT cfg_clipboard_type; | |
131 | ||
132 | /* The current state for delayed rendering. */ | |
133 | static Lisp_Object current_text; | |
134 | static Lisp_Object current_coding_system; | |
135 | static int current_requires_encoding, current_num_nls; | |
136 | static UINT current_clipboard_type; | |
137 | static LCID current_lcid; | |
138 | ||
139 | #if TRACE | |
140 | #define ONTRACE(stmt) stmt | |
141 | #else | |
142 | #define ONTRACE(stmt) /*stmt*/ | |
143 | #endif | |
144 | ||
145 | ||
146 | /* This function assumes that there is no multibyte character in | |
147 | current_text, so we can short-cut encoding. */ | |
148 | ||
149 | static HGLOBAL | |
150 | convert_to_handle_as_ascii (void) | |
ee78dc32 | 151 | { |
52c7f9ee JR |
152 | HGLOBAL htext = NULL; |
153 | int nbytes; | |
154 | int truelen; | |
155 | unsigned char *src; | |
156 | unsigned char *dst; | |
41a48e45 | 157 | |
52c7f9ee | 158 | ONTRACE (fprintf (stderr, "convert_to_handle_as_ascii\n")); |
41a48e45 | 159 | |
52c7f9ee JR |
160 | nbytes = SBYTES (current_text) + 1; |
161 | src = SDATA (current_text); | |
41a48e45 | 162 | |
52c7f9ee JR |
163 | /* We need to add to the size the number of LF chars where we have |
164 | to insert CR chars (the standard CF_TEXT clipboard format uses | |
165 | CRLF line endings, while Emacs uses just LF internally). */ | |
41a48e45 | 166 | |
52c7f9ee JR |
167 | truelen = nbytes + current_num_nls; |
168 | ||
169 | if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, truelen)) == NULL) | |
170 | return NULL; | |
41a48e45 | 171 | |
52c7f9ee JR |
172 | if ((dst = (unsigned char *) GlobalLock (htext)) == NULL) |
173 | { | |
174 | GlobalFree (htext); | |
175 | return NULL; | |
176 | } | |
177 | ||
178 | /* convert to CRLF line endings expected by clipboard */ | |
179 | while (1) | |
180 | { | |
181 | unsigned char *next; | |
182 | /* copy next line or remaining bytes including '\0' */ | |
183 | next = _memccpy (dst, src, '\n', nbytes); | |
184 | if (next) | |
185 | { | |
186 | /* copied one line ending with '\n' */ | |
187 | int copied = next - dst; | |
188 | nbytes -= copied; | |
189 | src += copied; | |
190 | /* insert '\r' before '\n' */ | |
191 | next[-1] = '\r'; | |
192 | next[0] = '\n'; | |
193 | dst = next + 1; | |
194 | } | |
195 | else | |
196 | /* copied remaining partial line -> now finished */ | |
197 | break; | |
198 | } | |
199 | ||
200 | GlobalUnlock (htext); | |
201 | ||
202 | return htext; | |
ee78dc32 GV |
203 | } |
204 | ||
52c7f9ee JR |
205 | /* This function assumes that there are multibyte or NUL characters in |
206 | current_text, or that we need to construct Unicode. It runs the | |
207 | text through the encoding machinery. */ | |
208 | ||
209 | static HGLOBAL | |
210 | convert_to_handle_as_coded (Lisp_Object coding_system) | |
ee78dc32 | 211 | { |
9a38f8d5 | 212 | HGLOBAL htext; |
52c7f9ee | 213 | unsigned char *dst = NULL; |
52c7f9ee | 214 | struct coding_system coding; |
52c7f9ee | 215 | |
b56ceb92 | 216 | ONTRACE (fprintf (stderr, "convert_to_handle_as_coded: %s\n", |
52c7f9ee JR |
217 | SDATA (SYMBOL_NAME (coding_system)))); |
218 | ||
9a38f8d5 | 219 | setup_windows_coding_system (coding_system, &coding); |
ed3751c8 | 220 | coding.dst_bytes = SBYTES (current_text) * 2; |
9a38f8d5 KH |
221 | coding.destination = (unsigned char *) xmalloc (coding.dst_bytes); |
222 | encode_coding_object (&coding, current_text, 0, 0, | |
223 | SCHARS (current_text), SBYTES (current_text), Qnil); | |
41a48e45 | 224 | |
9a38f8d5 | 225 | htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, coding.produced +2); |
41a48e45 | 226 | |
52c7f9ee JR |
227 | if (htext != NULL) |
228 | dst = (unsigned char *) GlobalLock (htext); | |
41a48e45 | 229 | |
52c7f9ee JR |
230 | if (dst != NULL) |
231 | { | |
9a38f8d5 | 232 | memcpy (dst, coding.destination, coding.produced); |
52c7f9ee JR |
233 | /* Add the string terminator. Add two NULs in case we are |
234 | producing Unicode here. */ | |
235 | dst[coding.produced] = dst[coding.produced+1] = '\0'; | |
52c7f9ee | 236 | |
9a38f8d5 | 237 | GlobalUnlock (htext); |
52c7f9ee JR |
238 | } |
239 | ||
9a38f8d5 KH |
240 | xfree (coding.destination); |
241 | ||
52c7f9ee | 242 | return htext; |
ee78dc32 GV |
243 | } |
244 | ||
52c7f9ee JR |
245 | static Lisp_Object |
246 | render (Lisp_Object oformat) | |
ee78dc32 | 247 | { |
52c7f9ee JR |
248 | HGLOBAL htext = NULL; |
249 | UINT format = XFASTINT (oformat); | |
250 | ||
251 | ONTRACE (fprintf (stderr, "render\n")); | |
252 | ||
253 | if (NILP (current_text)) | |
254 | return Qnil; | |
255 | ||
256 | if (current_requires_encoding || format == CF_UNICODETEXT) | |
257 | { | |
258 | if (format == current_clipboard_type) | |
259 | htext = convert_to_handle_as_coded (current_coding_system); | |
260 | else | |
261 | switch (format) | |
262 | { | |
263 | case CF_UNICODETEXT: | |
264 | htext = convert_to_handle_as_coded (QUNICODE); | |
265 | break; | |
266 | case CF_TEXT: | |
267 | case CF_OEMTEXT: | |
268 | { | |
269 | Lisp_Object cs; | |
270 | cs = coding_from_cp (cp_from_locale (current_lcid, format)); | |
271 | htext = convert_to_handle_as_coded (cs); | |
272 | break; | |
273 | } | |
274 | } | |
275 | } | |
276 | else | |
277 | htext = convert_to_handle_as_ascii (); | |
278 | ||
279 | ONTRACE (fprintf (stderr, "render: htext = 0x%08X\n", (unsigned) htext)); | |
280 | ||
281 | if (htext == NULL) | |
282 | return Qnil; | |
283 | ||
284 | if (SetClipboardData (format, htext) == NULL) | |
285 | { | |
ed3751c8 | 286 | GlobalFree (htext); |
52c7f9ee JR |
287 | return Qnil; |
288 | } | |
289 | ||
290 | return Qt; | |
ee78dc32 GV |
291 | } |
292 | ||
52c7f9ee JR |
293 | static Lisp_Object |
294 | render_locale (void) | |
ee78dc32 | 295 | { |
52c7f9ee JR |
296 | HANDLE hlocale = NULL; |
297 | LCID * lcid_ptr; | |
298 | ||
299 | ONTRACE (fprintf (stderr, "render_locale\n")); | |
300 | ||
301 | if (current_lcid == LOCALE_NEUTRAL || current_lcid == DEFAULT_LCID) | |
302 | return Qt; | |
303 | ||
304 | hlocale = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, sizeof (current_lcid)); | |
305 | if (hlocale == NULL) | |
306 | return Qnil; | |
307 | ||
308 | if ((lcid_ptr = (LCID *) GlobalLock (hlocale)) == NULL) | |
309 | { | |
ed3751c8 | 310 | GlobalFree (hlocale); |
52c7f9ee JR |
311 | return Qnil; |
312 | } | |
313 | ||
314 | *lcid_ptr = current_lcid; | |
315 | GlobalUnlock (hlocale); | |
316 | ||
317 | if (SetClipboardData (CF_LOCALE, hlocale) == NULL) | |
318 | { | |
ed3751c8 | 319 | GlobalFree (hlocale); |
52c7f9ee JR |
320 | return Qnil; |
321 | } | |
322 | ||
323 | return Qt; | |
324 | } | |
325 | ||
326 | /* At the end of the program, we want to ensure that our clipboard | |
327 | data survives us. This code will do that. */ | |
328 | ||
329 | static Lisp_Object | |
b56ceb92 | 330 | render_all (Lisp_Object ignore) |
52c7f9ee JR |
331 | { |
332 | ONTRACE (fprintf (stderr, "render_all\n")); | |
333 | ||
334 | /* According to the docs we should not call OpenClipboard() here, | |
335 | but testing on W2K and working code in other projects shows that | |
336 | it is actually necessary. */ | |
337 | ||
338 | OpenClipboard (NULL); | |
339 | ||
340 | /* There is no usefull means to report errors here, there are none | |
341 | expected anyway, and even if there were errors, they wouldn't do | |
342 | any harm. So we just go ahead and do what has to be done without | |
343 | bothering with error handling. */ | |
344 | ||
345 | ++modifying_clipboard; | |
346 | EmptyClipboard (); | |
347 | --modifying_clipboard; | |
348 | ||
349 | /* For text formats that we don't render here, the OS can use its | |
350 | own translation rules instead, so we don't really need to offer | |
351 | everything. To minimize memory consumption we cover three | |
352 | possible situations based on our primary format as detected from | |
353 | selection-coding-system (see setup_config()): | |
354 | ||
355 | - Post CF_TEXT only. Let the OS convert to CF_OEMTEXT and the OS | |
356 | (on NT) or the application (on 9x/Me) convert to | |
357 | CF_UNICODETEXT. | |
358 | ||
359 | - Post CF_OEMTEXT only. Similar automatic conversions happen as | |
360 | for CF_TEXT. | |
361 | ||
362 | - Post CF_UNICODETEXT + CF_TEXT. 9x itself ignores | |
363 | CF_UNICODETEXT, even though some applications can still handle | |
364 | it. | |
365 | ||
366 | Note 1: We render the less capable CF_TEXT *before* the more | |
367 | capable CF_UNICODETEXT, to prevent clobbering through automatic | |
368 | conversions, just in case. | |
369 | ||
370 | Note 2: We could check os_subtype here and only render the | |
371 | additional CF_TEXT on 9x/Me. But OTOH with | |
372 | current_clipboard_type == CF_UNICODETEXT we don't involve the | |
373 | automatic conversions anywhere else, so to get consistent | |
374 | results, we probably don't want to rely on it here either. */ | |
375 | ||
ed3751c8 | 376 | render_locale (); |
52c7f9ee JR |
377 | |
378 | if (current_clipboard_type == CF_UNICODETEXT) | |
379 | render (make_number (CF_TEXT)); | |
380 | render (make_number (current_clipboard_type)); | |
381 | ||
382 | CloseClipboard (); | |
383 | ||
384 | return Qnil; | |
385 | } | |
386 | ||
387 | static void | |
b56ceb92 | 388 | run_protected (Lisp_Object (*code) (Lisp_Object), Lisp_Object arg) |
52c7f9ee JR |
389 | { |
390 | /* FIXME: This works but it doesn't feel right. Too much fiddling | |
391 | with global variables and calling strange looking functions. Is | |
392 | this really the right way to run Lisp callbacks? */ | |
393 | ||
52c7f9ee | 394 | int owfi; |
41a48e45 | 395 | |
ee78dc32 | 396 | BLOCK_INPUT; |
41a48e45 | 397 | |
52c7f9ee JR |
398 | /* Fsignal calls abort() if it sees that waiting_for_input is |
399 | set. */ | |
400 | owfi = waiting_for_input; | |
401 | waiting_for_input = 0; | |
402 | ||
403 | internal_condition_case_1 (code, arg, Qt, lisp_error_handler); | |
404 | ||
405 | waiting_for_input = owfi; | |
41a48e45 | 406 | |
ee78dc32 | 407 | UNBLOCK_INPUT; |
52c7f9ee | 408 | } |
41a48e45 | 409 | |
52c7f9ee JR |
410 | static Lisp_Object |
411 | lisp_error_handler (Lisp_Object error) | |
412 | { | |
413 | Vsignaling_function = Qnil; | |
414 | cmd_error_internal (error, "Error in delayed clipboard rendering: "); | |
415 | Vinhibit_quit = Qt; | |
416 | return Qt; | |
ee78dc32 GV |
417 | } |
418 | ||
ee78dc32 | 419 | |
52c7f9ee JR |
420 | static LRESULT CALLBACK |
421 | owner_callback (HWND win, UINT msg, WPARAM wp, LPARAM lp) | |
ee78dc32 | 422 | { |
52c7f9ee JR |
423 | switch (msg) |
424 | { | |
425 | case WM_RENDERFORMAT: | |
426 | ONTRACE (fprintf (stderr, "WM_RENDERFORMAT\n")); | |
427 | run_protected (render, make_number (wp)); | |
428 | return 0; | |
429 | ||
430 | case WM_RENDERALLFORMATS: | |
431 | ONTRACE (fprintf (stderr, "WM_RENDERALLFORMATS\n")); | |
432 | run_protected (render_all, Qnil); | |
433 | return 0; | |
434 | ||
435 | case WM_DESTROYCLIPBOARD: | |
436 | if (!modifying_clipboard) | |
437 | { | |
438 | ONTRACE (fprintf (stderr, "WM_DESTROYCLIPBOARD (other)\n")); | |
439 | current_text = Qnil; | |
440 | current_coding_system = Qnil; | |
441 | } | |
442 | else | |
443 | { | |
444 | ONTRACE (fprintf (stderr, "WM_DESTROYCLIPBOARD (self)\n")); | |
445 | } | |
446 | return 0; | |
0ece9ef6 | 447 | |
52c7f9ee JR |
448 | case WM_DESTROY: |
449 | if (win == clipboard_owner) | |
450 | clipboard_owner = NULL; | |
451 | break; | |
452 | } | |
41a48e45 | 453 | |
52c7f9ee JR |
454 | return DefWindowProc (win, msg, wp, lp); |
455 | } | |
41a48e45 | 456 | |
52c7f9ee JR |
457 | static HWND |
458 | create_owner (void) | |
459 | { | |
460 | static const char CLASSNAME[] = "Emacs Clipboard"; | |
461 | WNDCLASS wc; | |
69cddef0 | 462 | |
52c7f9ee JR |
463 | memset (&wc, 0, sizeof (wc)); |
464 | wc.lpszClassName = CLASSNAME; | |
465 | wc.lpfnWndProc = owner_callback; | |
466 | RegisterClass (&wc); | |
467 | ||
468 | return CreateWindow (CLASSNAME, CLASSNAME, 0, 0, 0, 0, 0, NULL, NULL, | |
469 | NULL, NULL); | |
470 | } | |
471 | ||
472 | /* Called on exit by term_ntproc() in w32.c */ | |
473 | ||
474 | void | |
475 | term_w32select (void) | |
476 | { | |
477 | /* This is needed to trigger WM_RENDERALLFORMATS. */ | |
478 | if (clipboard_owner != NULL) | |
479 | DestroyWindow (clipboard_owner); | |
480 | } | |
0ece9ef6 | 481 | |
52c7f9ee JR |
482 | static void |
483 | setup_config (void) | |
484 | { | |
485 | const char *coding_name; | |
486 | const char *cp; | |
487 | char *end; | |
488 | int slen; | |
9a38f8d5 KH |
489 | Lisp_Object coding_system; |
490 | Lisp_Object dos_coding_system; | |
52c7f9ee JR |
491 | |
492 | CHECK_SYMBOL (Vselection_coding_system); | |
493 | ||
9a38f8d5 | 494 | coding_system = NILP (Vnext_selection_coding_system) ? |
52c7f9ee | 495 | Vselection_coding_system : Vnext_selection_coding_system; |
9a38f8d5 KH |
496 | |
497 | dos_coding_system = validate_coding_system (coding_system); | |
498 | if (NILP (dos_coding_system)) | |
499 | Fsignal (Qerror, | |
500 | list2 (build_string ("Coding system is invalid or doesn't have " | |
501 | "an eol variant for dos line ends"), | |
502 | coding_system)); | |
503 | ||
504 | /* Check if we have it cached */ | |
52c7f9ee | 505 | if (!NILP (cfg_coding_system) |
9a38f8d5 | 506 | && EQ (cfg_coding_system, dos_coding_system)) |
52c7f9ee | 507 | return; |
9a38f8d5 | 508 | cfg_coding_system = dos_coding_system; |
b56ceb92 | 509 | |
52c7f9ee JR |
510 | /* Set some sensible fallbacks */ |
511 | cfg_codepage = ANSICP; | |
512 | cfg_lcid = LOCALE_NEUTRAL; | |
513 | cfg_clipboard_type = CF_TEXT; | |
514 | ||
515 | /* Interpret the coding system symbol name */ | |
516 | coding_name = SDATA (SYMBOL_NAME (cfg_coding_system)); | |
517 | ||
518 | /* "(.*-)?utf-16.*" -> CF_UNICODETEXT */ | |
519 | cp = strstr (coding_name, "utf-16"); | |
520 | if (cp != NULL && (cp == coding_name || cp[-1] == '-')) | |
0ece9ef6 | 521 | { |
52c7f9ee JR |
522 | cfg_clipboard_type = CF_UNICODETEXT; |
523 | return; | |
0ece9ef6 | 524 | } |
69cddef0 | 525 | |
52c7f9ee JR |
526 | /* "cp[0-9]+.*" or "windows-[0-9]+.*" -> CF_TEXT or CF_OEMTEXT */ |
527 | slen = strlen (coding_name); | |
528 | if (slen >= 4 && coding_name[0] == 'c' && coding_name[1] == 'p') | |
529 | cp = coding_name + 2; | |
530 | else if (slen >= 10 && memcmp (coding_name, "windows-", 8) == 0) | |
531 | cp = coding_name + 8; | |
532 | else | |
533 | return; | |
534 | ||
535 | end = (char*)cp; | |
536 | cfg_codepage = strtol (cp, &end, 10); | |
537 | ||
538 | /* Error return from strtol() or number of digits < 2 -> Restore the | |
539 | default and drop it. */ | |
540 | if (cfg_codepage == 0 || (end-cp) < 2 ) | |
541 | { | |
542 | cfg_codepage = ANSICP; | |
543 | return; | |
544 | } | |
bbb059f3 | 545 | |
52c7f9ee JR |
546 | /* Is it the currently active system default? */ |
547 | if (cfg_codepage == ANSICP) | |
548 | { | |
549 | /* cfg_clipboard_type = CF_TEXT; */ | |
550 | return; | |
551 | } | |
552 | if (cfg_codepage == OEMCP) | |
553 | { | |
554 | cfg_clipboard_type = CF_OEMTEXT; | |
555 | return; | |
556 | } | |
69cddef0 | 557 | |
52c7f9ee JR |
558 | /* Else determine a suitable locale the hard way. */ |
559 | EnumSystemLocales (enum_locale_callback, LCID_INSTALLED); | |
560 | } | |
69cddef0 | 561 | |
52c7f9ee JR |
562 | static BOOL WINAPI |
563 | enum_locale_callback (/*const*/ char* loc_string) | |
564 | { | |
565 | LCID lcid; | |
566 | UINT codepage; | |
69cddef0 | 567 | |
52c7f9ee | 568 | lcid = strtoul (loc_string, NULL, 16); |
bbb059f3 | 569 | |
52c7f9ee JR |
570 | /* Is the wanted codepage the "ANSI" codepage for this locale? */ |
571 | codepage = cp_from_locale (lcid, CF_TEXT); | |
572 | if (codepage == cfg_codepage) | |
573 | { | |
574 | cfg_lcid = lcid; | |
575 | cfg_clipboard_type = CF_TEXT; | |
576 | return FALSE; /* Stop enumeration */ | |
577 | } | |
b56ceb92 | 578 | |
52c7f9ee JR |
579 | /* Is the wanted codepage the OEM codepage for this locale? */ |
580 | codepage = cp_from_locale (lcid, CF_OEMTEXT); | |
581 | if (codepage == cfg_codepage) | |
582 | { | |
583 | cfg_lcid = lcid; | |
584 | cfg_clipboard_type = CF_OEMTEXT; | |
585 | return FALSE; /* Stop enumeration */ | |
586 | } | |
41a48e45 | 587 | |
52c7f9ee JR |
588 | return TRUE; /* Continue enumeration */ |
589 | } | |
41a48e45 | 590 | |
52c7f9ee JR |
591 | static UINT |
592 | cp_from_locale (LCID lcid, UINT format) | |
593 | { | |
594 | char buffer[20] = ""; | |
595 | UINT variant, cp; | |
0108f679 | 596 | |
52c7f9ee JR |
597 | variant = |
598 | format == CF_TEXT ? LOCALE_IDEFAULTANSICODEPAGE : LOCALE_IDEFAULTCODEPAGE; | |
bbb059f3 | 599 | |
52c7f9ee JR |
600 | GetLocaleInfo (lcid, variant, buffer, sizeof (buffer)); |
601 | cp = strtoul (buffer, NULL, 10); | |
bf2133d7 | 602 | |
52c7f9ee JR |
603 | if (cp == CP_ACP) |
604 | return ANSICP; | |
605 | else if (cp == CP_OEMCP) | |
606 | return OEMCP; | |
607 | else | |
608 | return cp; | |
609 | } | |
bf2133d7 | 610 | |
52c7f9ee JR |
611 | static Lisp_Object |
612 | coding_from_cp (UINT codepage) | |
613 | { | |
614 | char buffer[30]; | |
615 | sprintf (buffer, "cp%d-dos", (int) codepage); | |
616 | return intern (buffer); | |
9a38f8d5 KH |
617 | /* We don't need to check that this coding system actually exists |
618 | right here, because that is done later for all coding systems | |
619 | used, regardless of where they originate. */ | |
ee78dc32 | 620 | } |
06466d9a | 621 | |
9a38f8d5 KH |
622 | static Lisp_Object |
623 | validate_coding_system (Lisp_Object coding_system) | |
624 | { | |
625 | Lisp_Object eol_type; | |
626 | ||
627 | /* Make sure the input is valid. */ | |
628 | if (NILP (Fcoding_system_p (coding_system))) | |
629 | return Qnil; | |
630 | ||
631 | /* Make sure we use a DOS coding system as mandated by the system | |
632 | specs. */ | |
633 | eol_type = Fcoding_system_eol_type (coding_system); | |
634 | ||
635 | /* Already a DOS coding system? */ | |
636 | if (EQ (eol_type, make_number (1))) | |
637 | return coding_system; | |
638 | ||
639 | /* Get EOL_TYPE vector of the base of CODING_SYSTEM. */ | |
640 | if (!VECTORP (eol_type)) | |
641 | { | |
642 | eol_type = Fcoding_system_eol_type (Fcoding_system_base (coding_system)); | |
643 | if (!VECTORP (eol_type)) | |
644 | return Qnil; | |
645 | } | |
646 | ||
647 | return AREF (eol_type, 1); | |
648 | } | |
649 | ||
650 | static void | |
651 | setup_windows_coding_system (Lisp_Object coding_system, | |
652 | struct coding_system * coding) | |
653 | { | |
654 | memset (coding, 0, sizeof (*coding)); | |
655 | setup_coding_system (coding_system, coding); | |
656 | ||
657 | /* Unset CODING_ANNOTATE_COMPOSITION_MASK. Previous code had | |
658 | comments about crashes in encode_coding_iso2022 trying to | |
659 | dereference a null pointer when composition was on. Selection | |
660 | data should not contain any composition sequence on Windows. | |
661 | ||
662 | CODING_ANNOTATION_MASK also includes | |
663 | CODING_ANNOTATE_DIRECTION_MASK and CODING_ANNOTATE_CHARSET_MASK, | |
664 | which both apply to ISO6429 only. We don't know if these really | |
665 | need to be unset on Windows, but it probably doesn't hurt | |
666 | either. */ | |
667 | coding->mode &= ~CODING_ANNOTATION_MASK; | |
668 | coding->mode |= CODING_MODE_LAST_BLOCK | CODING_MODE_SAFE_ENCODING; | |
669 | } | |
670 | ||
671 | ||
06466d9a | 672 | |
33f09670 JR |
673 | DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data, |
674 | Sw32_set_clipboard_data, 1, 2, 0, | |
675 | doc: /* This sets the clipboard data to the given text. */) | |
5842a27b | 676 | (Lisp_Object string, Lisp_Object ignored) |
ee78dc32 GV |
677 | { |
678 | BOOL ok = TRUE; | |
69cddef0 | 679 | int nbytes; |
69cddef0 GV |
680 | unsigned char *src; |
681 | unsigned char *dst; | |
52c7f9ee | 682 | unsigned char *end; |
06466d9a | 683 | |
52c7f9ee JR |
684 | /* This parameter used to be the current frame, but we don't use |
685 | that any more. */ | |
686 | (void) ignored; | |
41a48e45 | 687 | |
b7826503 | 688 | CHECK_STRING (string); |
41a48e45 | 689 | |
52c7f9ee | 690 | setup_config (); |
41a48e45 | 691 | |
52c7f9ee JR |
692 | current_text = string; |
693 | current_coding_system = cfg_coding_system; | |
694 | current_clipboard_type = cfg_clipboard_type; | |
695 | current_lcid = cfg_lcid; | |
696 | current_num_nls = 0; | |
697 | current_requires_encoding = 0; | |
b56ceb92 | 698 | |
ee78dc32 | 699 | BLOCK_INPUT; |
69cddef0 | 700 | |
52c7f9ee JR |
701 | /* Check for non-ASCII characters. While we are at it, count the |
702 | number of LFs, so we know how many CRs we will have to add later | |
703 | (just in the case where we can use our internal ASCII rendering, | |
704 | see code and comment in convert_to_handle_as_ascii() above). */ | |
705 | nbytes = SBYTES (string); | |
d5db4077 | 706 | src = SDATA (string); |
0ece9ef6 | 707 | |
52c7f9ee | 708 | for (dst = src, end = src+nbytes; dst < end; dst++) |
0ece9ef6 | 709 | { |
52c7f9ee JR |
710 | if (*dst == '\n') |
711 | current_num_nls++; | |
712 | else if (*dst >= 0x80 || *dst == 0) | |
713 | { | |
714 | current_requires_encoding = 1; | |
715 | break; | |
716 | } | |
0ece9ef6 | 717 | } |
69cddef0 | 718 | |
52c7f9ee JR |
719 | if (!current_requires_encoding) |
720 | { | |
721 | /* If all we have is ASCII we don't need to pretend we offer | |
722 | anything fancy. */ | |
723 | current_coding_system = Qraw_text; | |
724 | current_clipboard_type = CF_TEXT; | |
725 | current_lcid = LOCALE_NEUTRAL; | |
726 | } | |
0108f679 | 727 | |
52c7f9ee | 728 | if (!OpenClipboard (clipboard_owner)) |
ee78dc32 | 729 | goto error; |
06466d9a | 730 | |
52c7f9ee JR |
731 | ++modifying_clipboard; |
732 | ok = EmptyClipboard (); | |
733 | --modifying_clipboard; | |
06466d9a | 734 | |
52c7f9ee JR |
735 | /* If we have something non-ASCII we may want to set a locale. We |
736 | do that directly (non-delayed), as it's just a small bit. */ | |
737 | if (ok) | |
ed3751c8 | 738 | ok = !NILP (render_locale ()); |
06466d9a | 739 | |
52c7f9ee JR |
740 | if (ok) |
741 | { | |
742 | if (clipboard_owner == NULL) | |
743 | { | |
744 | /* If for some reason we don't have a clipboard_owner, we | |
745 | just set the text format as chosen by the configuration | |
746 | and than forget about the whole thing. */ | |
ed3751c8 | 747 | ok = !NILP (render (make_number (current_clipboard_type))); |
52c7f9ee JR |
748 | current_text = Qnil; |
749 | current_coding_system = Qnil; | |
750 | } | |
751 | else | |
752 | { | |
753 | /* Advertise all supported formats so that whatever the | |
754 | requester chooses, only one encoding step needs to be | |
755 | made. This is intentionally different from what we do in | |
756 | the handler for WM_RENDERALLFORMATS. */ | |
757 | SetClipboardData (CF_UNICODETEXT, NULL); | |
758 | SetClipboardData (CF_TEXT, NULL); | |
759 | SetClipboardData (CF_OEMTEXT, NULL); | |
760 | } | |
761 | } | |
41a48e45 | 762 | |
6383ca22 JR |
763 | CloseClipboard (); |
764 | ||
52c7f9ee JR |
765 | /* With delayed rendering we haven't really "used" this coding |
766 | system yet, and it's even unclear if we ever will. But this is a | |
767 | way to tell the upper level what we *would* use under ideal | |
768 | circumstances. | |
41a48e45 | 769 | |
52c7f9ee JR |
770 | We don't signal the actually used coding-system later when we |
771 | finally render, because that can happen at any time and we don't | |
772 | want to disturb the "foreground" action. */ | |
773 | if (ok) | |
774 | Vlast_coding_system_used = current_coding_system; | |
6383ca22 | 775 | |
52c7f9ee | 776 | Vnext_selection_coding_system = Qnil; |
ee79d1aa | 777 | |
ee78dc32 GV |
778 | if (ok) goto done; |
779 | ||
780 | error: | |
41a48e45 | 781 | |
ee78dc32 | 782 | ok = FALSE; |
52c7f9ee JR |
783 | current_text = Qnil; |
784 | current_coding_system = Qnil; | |
ee79d1aa | 785 | |
ee78dc32 GV |
786 | done: |
787 | UNBLOCK_INPUT; | |
41a48e45 | 788 | |
ee78dc32 GV |
789 | return (ok ? string : Qnil); |
790 | } | |
791 | ||
52c7f9ee | 792 | |
33f09670 JR |
793 | DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data, |
794 | Sw32_get_clipboard_data, 0, 1, 0, | |
795 | doc: /* This gets the clipboard data in text format. */) | |
5842a27b | 796 | (Lisp_Object ignored) |
ee78dc32 | 797 | { |
52c7f9ee | 798 | HGLOBAL htext; |
ee78dc32 | 799 | Lisp_Object ret = Qnil; |
52c7f9ee JR |
800 | UINT actual_clipboard_type; |
801 | int use_configured_coding_system = 1; | |
802 | ||
803 | /* This parameter used to be the current frame, but we don't use | |
804 | that any more. */ | |
805 | (void) ignored; | |
41a48e45 | 806 | |
52c7f9ee JR |
807 | /* Don't pass our own text from the clipboard (which might be |
808 | troublesome if the killed text includes null characters). */ | |
809 | if (!NILP (current_text)) | |
810 | return ret; | |
811 | ||
812 | setup_config (); | |
813 | actual_clipboard_type = cfg_clipboard_type; | |
41a48e45 | 814 | |
ee78dc32 | 815 | BLOCK_INPUT; |
41a48e45 | 816 | |
52c7f9ee | 817 | if (!OpenClipboard (clipboard_owner)) |
ee78dc32 | 818 | goto done; |
41a48e45 | 819 | |
52c7f9ee JR |
820 | if ((htext = GetClipboardData (actual_clipboard_type)) == NULL) |
821 | { | |
822 | /* If we want CF_UNICODETEXT but can't get it, the current | |
823 | coding system is useless. OTOH we can still try and decode | |
824 | CF_TEXT based on the locale that the system gives us and that | |
825 | we get down below. */ | |
826 | if (actual_clipboard_type == CF_UNICODETEXT) | |
827 | { | |
828 | htext = GetClipboardData (CF_TEXT); | |
829 | if (htext != NULL) | |
830 | { | |
831 | actual_clipboard_type = CF_TEXT; | |
832 | use_configured_coding_system = 0; | |
833 | } | |
834 | } | |
835 | } | |
836 | if (htext == NULL) | |
ee78dc32 GV |
837 | goto closeclip; |
838 | ||
ee78dc32 | 839 | { |
69cddef0 GV |
840 | unsigned char *src; |
841 | unsigned char *dst; | |
ee78dc32 | 842 | int nbytes; |
69cddef0 | 843 | int truelen; |
c0ca703b | 844 | int require_decoding = 0; |
41a48e45 | 845 | |
69cddef0 | 846 | if ((src = (unsigned char *) GlobalLock (htext)) == NULL) |
ee78dc32 | 847 | goto closeclip; |
41a48e45 | 848 | |
52c7f9ee JR |
849 | /* If the clipboard data contains any non-ascii code, we need to |
850 | decode it with a coding system. */ | |
851 | if (actual_clipboard_type == CF_UNICODETEXT) | |
852 | { | |
853 | nbytes = lstrlenW ((WCHAR *)src) * 2; | |
854 | require_decoding = 1; | |
855 | } | |
856 | else | |
857 | { | |
858 | int i; | |
06466d9a | 859 | |
52c7f9ee | 860 | nbytes = strlen (src); |
aab7e392 | 861 | |
52c7f9ee JR |
862 | for (i = 0; i < nbytes; i++) |
863 | { | |
864 | if (src[i] >= 0x80) | |
865 | { | |
866 | require_decoding = 1; | |
867 | break; | |
868 | } | |
869 | } | |
870 | } | |
93cbf229 | 871 | |
c0ca703b | 872 | if (require_decoding) |
69cddef0 | 873 | { |
bbb059f3 | 874 | struct coding_system coding; |
52c7f9ee | 875 | Lisp_Object coding_system = Qnil; |
9a38f8d5 | 876 | Lisp_Object dos_coding_system; |
b56ceb92 | 877 | |
52c7f9ee JR |
878 | /* `next-selection-coding-system' should override everything, |
879 | even when the locale passed by the system disagrees. The | |
880 | only exception is when `next-selection-coding-system' | |
881 | requested CF_UNICODETEXT and we couldn't get that. */ | |
882 | if (use_configured_coding_system | |
883 | && !NILP (Vnext_selection_coding_system)) | |
884 | coding_system = Vnext_selection_coding_system; | |
885 | ||
886 | /* If we have CF_TEXT or CF_OEMTEXT, we want to check out | |
887 | CF_LOCALE, too. */ | |
888 | else if (actual_clipboard_type != CF_UNICODETEXT) | |
889 | { | |
890 | HGLOBAL hlocale; | |
891 | LCID lcid = DEFAULT_LCID; | |
892 | UINT cp; | |
893 | ||
894 | /* Documentation says that the OS always generates | |
895 | CF_LOCALE info automatically, so the locale handle | |
896 | should always be present. Fact is that this is not | |
897 | always true on 9x ;-(. */ | |
898 | hlocale = GetClipboardData (CF_LOCALE); | |
899 | if (hlocale != NULL) | |
900 | { | |
901 | const LCID * lcid_ptr; | |
902 | lcid_ptr = (const LCID *) GlobalLock (hlocale); | |
903 | if (lcid_ptr != NULL) | |
904 | { | |
905 | lcid = *lcid_ptr; | |
906 | GlobalUnlock (hlocale); | |
907 | } | |
908 | ||
909 | /* 9x has garbage as the sort order (to be exact there | |
910 | is another instance of the language id in the upper | |
911 | word). We don't care about sort order anyway, so | |
912 | we just filter out the unneeded mis-information to | |
913 | avoid irritations. */ | |
914 | lcid = MAKELCID (LANGIDFROMLCID (lcid), SORT_DEFAULT); | |
915 | } | |
916 | ||
917 | /* If we are using fallback from CF_UNICODETEXT, we can't | |
918 | use the configured coding system. Also we don't want | |
919 | to use it, if the system has supplied us with a locale | |
920 | and it is not just the system default. */ | |
921 | if (!use_configured_coding_system || lcid != DEFAULT_LCID) | |
922 | { | |
923 | cp = cp_from_locale (lcid, actual_clipboard_type); | |
924 | /* If it's just our current standard setting anyway, | |
925 | use the coding system that the user has selected. | |
926 | Otherwise create a new spec to match the locale | |
927 | that was specified by the other side or the | |
928 | system. */ | |
929 | if (!use_configured_coding_system || cp != cfg_codepage) | |
930 | coding_system = coding_from_cp (cp); | |
931 | } | |
932 | } | |
933 | ||
934 | if (NILP (coding_system)) | |
935 | coding_system = Vselection_coding_system; | |
936 | Vnext_selection_coding_system = Qnil; | |
bbb059f3 | 937 | |
9a38f8d5 KH |
938 | dos_coding_system = validate_coding_system (coding_system); |
939 | if (!NILP (dos_coding_system)) | |
940 | { | |
941 | setup_windows_coding_system (dos_coding_system, &coding); | |
942 | coding.source = src; | |
943 | decode_coding_object (&coding, Qnil, 0, 0, nbytes, nbytes, Qt); | |
944 | ret = coding.dst_object; | |
945 | ||
946 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
947 | } | |
69cddef0 | 948 | } |
bbb059f3 AI |
949 | else |
950 | { | |
52c7f9ee JR |
951 | /* FIXME: We may want to repeat the code in this branch for |
952 | the Unicode case. */ | |
953 | ||
954 | /* Need to know final size after CR chars are removed because | |
955 | we can't change the string size manually, and doing an | |
956 | extra copy is silly. We only remove CR when it appears as | |
957 | part of CRLF. */ | |
bbb059f3 AI |
958 | |
959 | truelen = nbytes; | |
960 | dst = src; | |
961 | /* avoid using strchr because it recomputes the length everytime */ | |
962 | while ((dst = memchr (dst, '\r', nbytes - (dst - src))) != NULL) | |
963 | { | |
c0ca703b GV |
964 | if (dst[1] == '\n') /* safe because of trailing '\0' */ |
965 | truelen--; | |
bbb059f3 AI |
966 | dst++; |
967 | } | |
69cddef0 | 968 | |
bbb059f3 | 969 | ret = make_uninit_string (truelen); |
69cddef0 | 970 | |
c0ca703b GV |
971 | /* Convert CRLF line endings (the standard CF_TEXT clipboard |
972 | format) to LF endings as used internally by Emacs. */ | |
69cddef0 | 973 | |
d5db4077 | 974 | dst = SDATA (ret); |
bbb059f3 | 975 | while (1) |
69cddef0 | 976 | { |
bbb059f3 AI |
977 | unsigned char *next; |
978 | /* copy next line or remaining bytes excluding '\0' */ | |
979 | next = _memccpy (dst, src, '\r', nbytes); | |
980 | if (next) | |
981 | { | |
982 | /* copied one line ending with '\r' */ | |
983 | int copied = next - dst; | |
984 | nbytes -= copied; | |
c0ca703b | 985 | dst += copied; |
bbb059f3 | 986 | src += copied; |
c0ca703b GV |
987 | if (*src == '\n') |
988 | dst--; /* overwrite '\r' with '\n' */ | |
989 | } | |
bbb059f3 AI |
990 | else |
991 | /* copied remaining partial line -> now finished */ | |
992 | break; | |
993 | } | |
0108f679 AI |
994 | |
995 | Vlast_coding_system_used = Qraw_text; | |
69cddef0 GV |
996 | } |
997 | ||
ee78dc32 GV |
998 | GlobalUnlock (htext); |
999 | } | |
1000 | ||
1001 | closeclip: | |
1002 | CloseClipboard (); | |
41a48e45 | 1003 | |
ee78dc32 GV |
1004 | done: |
1005 | UNBLOCK_INPUT; | |
41a48e45 | 1006 | |
ee78dc32 GV |
1007 | return (ret); |
1008 | } | |
1009 | ||
9aa94bd5 KH |
1010 | /* Support checking for a clipboard selection. */ |
1011 | ||
1012 | DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, | |
33f09670 JR |
1013 | 0, 1, 0, |
1014 | doc: /* Whether there is an owner for the given X Selection. | |
1015 | The arg should be the name of the selection in question, typically one of | |
1016 | the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. | |
1017 | \(Those are literal upper-case symbol names, since that's what X expects.) | |
1018 | For convenience, the symbol nil is the same as `PRIMARY', | |
1019 | and t is the same as `SECONDARY'. */) | |
5842a27b | 1020 | (Lisp_Object selection) |
9aa94bd5 | 1021 | { |
b7826503 | 1022 | CHECK_SYMBOL (selection); |
9aa94bd5 KH |
1023 | |
1024 | /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check | |
1025 | if the clipboard currently has valid text format contents. */ | |
1026 | ||
1027 | if (EQ (selection, QCLIPBOARD)) | |
1028 | { | |
1029 | Lisp_Object val = Qnil; | |
1030 | ||
9a38f8d5 KH |
1031 | setup_config (); |
1032 | ||
9aa94bd5 KH |
1033 | if (OpenClipboard (NULL)) |
1034 | { | |
52c7f9ee | 1035 | UINT format = 0; |
52c7f9ee JR |
1036 | while ((format = EnumClipboardFormats (format))) |
1037 | /* Check CF_TEXT in addition to cfg_clipboard_type, | |
1038 | because we can fall back on that if CF_UNICODETEXT is | |
1039 | not available. Actually a check for CF_TEXT only | |
1040 | should be enough. */ | |
1041 | if (format == cfg_clipboard_type || format == CF_TEXT) | |
9aa94bd5 KH |
1042 | { |
1043 | val = Qt; | |
1044 | break; | |
1045 | } | |
1046 | CloseClipboard (); | |
1047 | } | |
1048 | return val; | |
1049 | } | |
1050 | return Qnil; | |
1051 | } | |
1052 | ||
52c7f9ee JR |
1053 | /* One-time init. Called in the un-dumped Emacs, but not in the |
1054 | dumped version. */ | |
1055 | ||
41a48e45 | 1056 | void |
b56ceb92 | 1057 | syms_of_w32select (void) |
ee78dc32 | 1058 | { |
fbd6baed GV |
1059 | defsubr (&Sw32_set_clipboard_data); |
1060 | defsubr (&Sw32_get_clipboard_data); | |
9aa94bd5 KH |
1061 | defsubr (&Sx_selection_exists_p); |
1062 | ||
29208e82 | 1063 | DEFVAR_LISP ("selection-coding-system", Vselection_coding_system, |
33f09670 | 1064 | doc: /* Coding system for communicating with other programs. |
f5f25615 GM |
1065 | |
1066 | For MS-Windows and MS-DOS: | |
1067 | When sending or receiving text via selection and clipboard, the text | |
1068 | is encoded or decoded by this coding system. The default value is | |
1069 | the current system default encoding on 9x/Me, `utf-16le-dos' | |
1070 | \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS. | |
1071 | ||
1072 | For X Windows: | |
1073 | When sending text via selection and clipboard, if the target | |
1074 | data-type matches with the type of this coding system, it is used | |
1075 | for encoding the text. Otherwise (including the case that this | |
1076 | variable is nil), a proper coding system is used as below: | |
1077 | ||
1078 | data-type coding system | |
1079 | --------- ------------- | |
1080 | UTF8_STRING utf-8 | |
1081 | COMPOUND_TEXT compound-text-with-extensions | |
1082 | STRING iso-latin-1 | |
1083 | C_STRING no-conversion | |
1084 | ||
1085 | When receiving text, if this coding system is non-nil, it is used | |
1086 | for decoding regardless of the data-type. If this is nil, a | |
1087 | proper coding system is used according to the data-type as above. | |
1088 | ||
1089 | See also the documentation of the variable `x-select-request-type' how | |
1090 | to control which data-type to request for receiving text. | |
1091 | ||
1092 | The default value is nil. */); | |
52c7f9ee JR |
1093 | /* The actual value is set dynamically in the dumped Emacs, see |
1094 | below. */ | |
1095 | Vselection_coding_system = Qnil; | |
bbb059f3 | 1096 | |
29208e82 | 1097 | DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system, |
33f09670 JR |
1098 | doc: /* Coding system for the next communication with other programs. |
1099 | Usually, `selection-coding-system' is used for communicating with | |
3646b86d GM |
1100 | other programs (X Windows clients or MS Windows programs). But, if this |
1101 | variable is set, it is used for the next communication only. | |
1102 | After the communication, this variable is set to nil. */); | |
93cbf229 GV |
1103 | Vnext_selection_coding_system = Qnil; |
1104 | ||
9c9d6d8b | 1105 | DEFSYM (QCLIPBOARD, "CLIPBOARD"); |
52c7f9ee JR |
1106 | |
1107 | cfg_coding_system = Qnil; staticpro (&cfg_coding_system); | |
1108 | current_text = Qnil; staticpro (¤t_text); | |
1109 | current_coding_system = Qnil; staticpro (¤t_coding_system); | |
1110 | ||
9c9d6d8b | 1111 | DEFSYM (QUNICODE, "utf-16le-dos"); |
52c7f9ee JR |
1112 | QANSICP = Qnil; staticpro (&QANSICP); |
1113 | QOEMCP = Qnil; staticpro (&QOEMCP); | |
1114 | } | |
1115 | ||
1116 | /* One-time init. Called in the dumped Emacs, but not in the | |
1117 | un-dumped version. */ | |
1118 | ||
1119 | void | |
b56ceb92 | 1120 | globals_of_w32select (void) |
52c7f9ee JR |
1121 | { |
1122 | DEFAULT_LCID = GetUserDefaultLCID (); | |
1123 | /* Drop the sort order from the LCID, so we can compare this with | |
1124 | CF_LOCALE objects that have the same fix on 9x. */ | |
1125 | DEFAULT_LCID = MAKELCID (LANGIDFROMLCID (DEFAULT_LCID), SORT_DEFAULT); | |
1126 | ||
1127 | ANSICP = GetACP (); | |
1128 | OEMCP = GetOEMCP (); | |
1129 | ||
1130 | QANSICP = coding_from_cp (ANSICP); | |
1131 | QOEMCP = coding_from_cp (OEMCP); | |
1132 | ||
1133 | if (os_subtype == OS_NT) | |
1134 | Vselection_coding_system = QUNICODE; | |
1135 | else if (inhibit_window_system) | |
1136 | Vselection_coding_system = QOEMCP; | |
1137 | else | |
1138 | Vselection_coding_system = QANSICP; | |
1139 | ||
1140 | clipboard_owner = create_owner (); | |
ee78dc32 | 1141 | } |
6b61353c | 1142 |