* nsterm.m (ns_dumpglyphs_image): dr is a new rect to draw image into,
[bpt/emacs.git] / src / w16select.c
CommitLineData
767079a8 1/* 16-bit Windows Selection processing for emacs on MS-Windows
e9bffc61 2
acaf905b 3Copyright (C) 1996-1997, 2001-2012 Free Software Foundation, Inc.
8de1855e 4
21cfcccf
EZ
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
21cfcccf 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
21cfcccf
EZ
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21cfcccf
EZ
19
20/* These functions work by using WinOldAp interface. WinOldAp
21 (WINOLDAP.MOD) is a Microsoft Windows extension supporting
22 "old" (character-mode) application access to Dynamic Data Exchange,
23 menus, and the Windows clipboard. */
24
25/* Written by Dale P. Smith <dpsm@en.com> */
ed68db4d 26/* Adapted to DJGPP by Eli Zaretskii <eliz@gnu.org> */
21cfcccf
EZ
27
28#ifdef MSDOS
29
30#include <config.h>
21cfcccf
EZ
31#include <dpmi.h>
32#include <go32.h>
33#include <sys/farptr.h>
34#include "lisp.h"
35#include "dispextern.h" /* frame.h seems to want this */
36#include "frame.h" /* Need this to get the X window of selected_frame */
37#include "blockinput.h"
83be827a 38#include "character.h"
e5560ff7 39#include "buffer.h"
583f5d53 40#include "coding.h"
f7d05dc4 41#include "composite.h"
21cfcccf
EZ
42
43/* If ever some function outside this file will need to call any
44 clipboard-related function, the following prototypes and constants
45 should be put on a header file. Right now, nobody else uses them. */
46
47#define CF_TEXT 0x01
48#define CF_BITMAP 0x02
49#define CF_METAFILE 0x03
50#define CF_SYLK 0x04
51#define CF_DIF 0x05
52#define CF_TIFF 0x06
53#define CF_OEMTEXT 0x07
54#define CF_DIBBITMAP 0x08
55#define CF_WINWRITE 0x80
56#define CF_DSPTEXT 0x81
57#define CF_DSPBITMAP 0x82
58
59unsigned identify_winoldap_version (void);
60unsigned open_clipboard (void);
61unsigned empty_clipboard (void);
583f5d53 62unsigned set_clipboard_data (unsigned, void *, unsigned, int);
21cfcccf 63unsigned get_clipboard_data_size (unsigned);
583f5d53 64unsigned get_clipboard_data (unsigned, void *, unsigned, int);
21cfcccf
EZ
65unsigned close_clipboard (void);
66unsigned clipboard_compact (unsigned);
67
68Lisp_Object QCLIPBOARD, QPRIMARY;
69
70/* The segment address and the size of the buffer in low
71 memory used to move data between us and WinOldAp module. */
21cfcccf
EZ
72static struct {
73 unsigned long size;
74 unsigned short rm_segment;
75} clipboard_xfer_buf_info;
5033894e
EZ
76
77/* The last text we put into the clipboard. This is used to prevent
78 passing back our own text from the clipboard, instead of using the
79 kill ring. The former is undesirable because the clipboard data
80 could be MULEtilated by inappropriately chosen
81 (next-)selection-coding-system. For this reason, we must store the
82 text *after* it was encoded/Unix-to-DOS-converted. */
83static unsigned char *last_clipboard_text;
84
85/* The size of allocated storage for storing the clipboard data. */
86static size_t clipboard_storage_size;
21cfcccf 87\f
21cfcccf
EZ
88/* C functions to access the Windows 3.1x clipboard from DOS apps.
89
90 The information was obtained from the Microsoft Knowledge Base,
91 article Q67675 and can be found at:
92 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
93
94/* See also Ralf Brown's Interrupt List.
95
96 I also seem to remember reading about this in Dr. Dobbs Journal a
97 while ago, but if you knew my memory... :-)
98
99 Dale P. Smith <dpsm@en.com> */
100
101/* Return the WinOldAp support version, or 0x1700 if not supported. */
102unsigned
3a8ce822 103identify_winoldap_version (void)
21cfcccf
EZ
104{
105 __dpmi_regs regs;
106
107 /* Calls Int 2Fh/AX=1700h
108 Return Values AX == 1700H: Clipboard functions not available
109 <> 1700H: AL = Major version number
110 AH = Minor version number */
111 regs.x.ax = 0x1700;
5e617bc2 112 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
113 return regs.x.ax;
114}
115
bbd240ce 116/* Open the clipboard, return non-zero if successful. */
21cfcccf 117unsigned
3a8ce822 118open_clipboard (void)
21cfcccf
EZ
119{
120 __dpmi_regs regs;
121
122 /* Is WINOLDAP supported? */
123 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
124 which is the same as ``Clipboard already open''. Currently,
125 this is taken as an error by all the functions that use
126 `open_clipboard', but if somebody someday will use that ``open''
127 clipboard, they will have interesting time debugging it... */
128 if (identify_winoldap_version () == 0x1700)
129 return 0;
130
131 /* Calls Int 2Fh/AX=1701h
132 Return Values AX == 0: Clipboard already open
133 <> 0: Clipboard opened */
134 regs.x.ax = 0x1701;
5e617bc2 135 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
136 return regs.x.ax;
137}
138
bbd240ce 139/* Empty clipboard, return non-zero if successful. */
21cfcccf 140unsigned
3a8ce822 141empty_clipboard (void)
21cfcccf
EZ
142{
143 __dpmi_regs regs;
8de1855e 144
21cfcccf
EZ
145 /* Calls Int 2Fh/AX=1702h
146 Return Values AX == 0: Error occurred
147 <> 0: OK, Clipboard emptied */
148 regs.x.ax = 0x1702;
5e617bc2 149 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
150 return regs.x.ax;
151}
152
153/* Ensure we have a buffer in low memory with enough memory for data
154 of size WANT_SIZE. Return the linear address of the buffer. */
155static unsigned long
3a8ce822 156alloc_xfer_buf (unsigned want_size)
21cfcccf
EZ
157{
158 __dpmi_regs regs;
159
160 /* If the usual DJGPP transfer buffer is large enough, use that. */
161 if (want_size <= _go32_info_block.size_of_transfer_buffer)
162 return __tb & 0xfffff;
163
767079a8
EZ
164 /* Don't even try to allocate more than 1MB of memory: DOS cannot
165 possibly handle that (it will overflow the BX register below). */
166 if (want_size > 0xfffff)
167 return 0;
168
21cfcccf
EZ
169 /* Need size rounded up to the nearest paragraph, and in
170 paragraph units (1 paragraph = 16 bytes). */
171 clipboard_xfer_buf_info.size = (want_size + 15) >> 4;
172
173 /* The NT DPMI host crashes us if we free DOS memory via the
174 DPMI service. Work around by calling DOS allocate/free block. */
175 regs.h.ah = 0x48;
176 regs.x.bx = clipboard_xfer_buf_info.size;
177 __dpmi_int (0x21, &regs);
178 if (regs.x.flags & 1)
179 {
180 clipboard_xfer_buf_info.size = 0;
181 return 0;
182 }
183
184 clipboard_xfer_buf_info.rm_segment = regs.x.ax;
185 return (((int)clipboard_xfer_buf_info.rm_segment) << 4) & 0xfffff;
186}
187
188/* Free our clipboard buffer. We always free it after use, because
189 keeping it leaves less free conventional memory for subprocesses.
190 The clipboard buffer tends to be large in size, because for small
191 clipboard data sizes we use the DJGPP transfer buffer. */
192static void
3a8ce822 193free_xfer_buf (void)
21cfcccf
EZ
194{
195 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
196 if (clipboard_xfer_buf_info.size)
197 {
198 __dpmi_regs regs;
199
200 /* The NT DPMI host crashes us if we free DOS memory via
201 the DPMI service. Work around by calling DOS free block. */
202 regs.h.ah = 0x49;
203 regs.x.es = clipboard_xfer_buf_info.rm_segment;
204 __dpmi_int (0x21, &regs);
205 clipboard_xfer_buf_info.size = 0;
206 }
207}
208
bbd240ce 209/* Copy data into the clipboard, return zero if successful. */
21cfcccf 210unsigned
3a8ce822 211set_clipboard_data (unsigned Format, void *Data, unsigned Size, int Raw)
21cfcccf
EZ
212{
213 __dpmi_regs regs;
214 unsigned truelen;
215 unsigned long xbuf_addr, buf_offset;
216 unsigned char *dp = Data, *dstart = dp;
217
538d05bb 218 if (Format != CF_OEMTEXT)
aae41d97 219 return 3;
21cfcccf
EZ
220
221 /* need to know final size after '\r' chars are inserted (the
538d05bb 222 standard CF_OEMTEXT clipboard format uses CRLF line endings,
21cfcccf 223 while Emacs uses just LF internally). */
0265f89f 224 truelen = Size + 1; /* +1 for the terminating null */
583f5d53
EZ
225
226 if (!Raw)
21cfcccf 227 {
583f5d53
EZ
228 /* avoid using strchr because it recomputes the length everytime */
229 while ((dp = memchr (dp, '\n', Size - (dp - dstart))) != 0)
230 {
231 truelen++;
232 dp++;
233 }
21cfcccf
EZ
234 }
235
236 if (clipboard_compact (truelen) < truelen)
aae41d97 237 return 1;
21cfcccf
EZ
238
239 if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0)
aae41d97 240 return 1;
21cfcccf 241
583f5d53
EZ
242 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
243 if (Raw)
0236e3f1
EZ
244 {
245 dosmemput (Data, Size, xbuf_addr);
246
247 /* Terminate with a null, otherwise Windows does strange things
248 when the text size is an integral multiple of 32 bytes. */
249 _farpokeb (_dos_ds, xbuf_addr + Size, '\0');
250 }
583f5d53 251 else
21cfcccf 252 {
583f5d53
EZ
253 dp = Data;
254 buf_offset = xbuf_addr;
255 _farsetsel (_dos_ds);
256 while (Size--)
257 {
0265f89f
EZ
258 /* Don't allow them to put binary data into the clipboard, since
259 it will cause yanked data to be truncated at the first null. */
260 if (*dp == '\0')
261 return 2;
583f5d53
EZ
262 if (*dp == '\n')
263 _farnspokeb (buf_offset++, '\r');
264 _farnspokeb (buf_offset++, *dp++);
265 }
21cfcccf 266
0236e3f1
EZ
267 /* Terminate with a null, otherwise Windows does strange things
268 when the text size is an integral multiple of 32 bytes. */
269 _farnspokeb (buf_offset, '\0');
270 }
0265f89f 271
5033894e
EZ
272 /* Stash away the data we are about to put into the clipboard, so we
273 could later check inside get_clipboard_data whether the clipboard
274 still holds our data. */
275 if (clipboard_storage_size < truelen)
276 {
277 clipboard_storage_size = truelen + 100;
278 last_clipboard_text =
279 (char *) xrealloc (last_clipboard_text, clipboard_storage_size);
280 }
281 if (last_clipboard_text)
282 dosmemget (xbuf_addr, truelen, last_clipboard_text);
283
21cfcccf
EZ
284 /* Calls Int 2Fh/AX=1703h with:
285 DX = WinOldAp-Supported Clipboard format
286 ES:BX = Pointer to data
287 SI:CX = Size of data in bytes
288 Return Values AX == 0: Error occurred
289 <> 0: OK. Data copied into the Clipboard. */
290 regs.x.ax = 0x1703;
291 regs.x.dx = Format;
292 regs.x.si = truelen >> 16;
293 regs.x.cx = truelen & 0xffff;
294 regs.x.es = xbuf_addr >> 4;
295 regs.x.bx = xbuf_addr & 15;
5e617bc2 296 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
297
298 free_xfer_buf ();
299
5033894e
EZ
300 /* If the above failed, invalidate the local copy of the clipboard. */
301 if (regs.x.ax == 0)
302 *last_clipboard_text = '\0';
303
aae41d97
EZ
304 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
305 return regs.x.ax > 0 ? 0 : 3;
21cfcccf
EZ
306}
307
308/* Return the size of the clipboard data of format FORMAT. */
309unsigned
3a8ce822 310get_clipboard_data_size (unsigned Format)
21cfcccf
EZ
311{
312 __dpmi_regs regs;
313
314 /* Calls Int 2Fh/AX=1704h with:
315 DX = WinOldAp-Supported Clipboard format
316 Return Values DX:AX == Size of the data in bytes, including any
317 headers.
318 == 0 If data in this format is not in
319 the clipboard. */
320 regs.x.ax = 0x1704;
321 regs.x.dx = Format;
5e617bc2 322 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
323 return ( (((unsigned)regs.x.dx) << 16) | regs.x.ax);
324}
325
326/* Get clipboard data, return its length.
327 Warning: this doesn't check whether DATA has enough space to hold
328 SIZE bytes. */
329unsigned
3a8ce822 330get_clipboard_data (unsigned Format, void *Data, unsigned Size, int Raw)
21cfcccf
EZ
331{
332 __dpmi_regs regs;
21cfcccf
EZ
333 unsigned long xbuf_addr;
334 unsigned char *dp = Data;
335
538d05bb 336 if (Format != CF_OEMTEXT)
21cfcccf
EZ
337 return 0;
338
339 if (Size == 0)
340 return 0;
341
342 if ((xbuf_addr = alloc_xfer_buf (Size)) == 0)
343 return 0;
344
345 /* Calls Int 2Fh/AX=1705h with:
346 DX = WinOldAp-Supported Clipboard format
347 ES:BX = Pointer to data buffer to hold data
348 Return Values AX == 0: Error occurred (or data in this format is not
349 in the clipboard)
350 <> 0: OK */
351 regs.x.ax = 0x1705;
352 regs.x.dx = Format;
353 regs.x.es = xbuf_addr >> 4;
354 regs.x.bx = xbuf_addr & 15;
5e617bc2 355 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
356 if (regs.x.ax != 0)
357 {
5033894e
EZ
358 unsigned char null_char = '\0';
359 unsigned long xbuf_beg = xbuf_addr;
360
361 /* If last_clipboard_text is NULL, we don't want to slow down
362 the next loop by an additional test. */
363 register unsigned char *lcdp =
364 last_clipboard_text == NULL ? &null_char : last_clipboard_text;
8de1855e 365
583f5d53
EZ
366 /* Copy data from low memory, remove CR
367 characters before LF if needed. */
21cfcccf
EZ
368 _farsetsel (_dos_ds);
369 while (Size--)
370 {
371 register unsigned char c = _farnspeekb (xbuf_addr++);
372
5033894e
EZ
373 if (*lcdp == c)
374 lcdp++;
375
583f5d53 376 if ((*dp++ = c) == '\r' && !Raw && _farnspeekb (xbuf_addr) == '\n')
21cfcccf
EZ
377 {
378 dp--;
379 *dp++ = '\n';
380 xbuf_addr++;
5033894e
EZ
381 if (*lcdp == '\n')
382 lcdp++;
21cfcccf
EZ
383 }
384 /* Windows reportedly rounds up the size of clipboard data
4029384b
EZ
385 (passed in SIZE) to a multiple of 32, and removes trailing
386 spaces from each line without updating SIZE. We therefore
387 bail out when we see the first null character. */
388 else if (c == '\0')
0265f89f 389 break;
21cfcccf 390 }
5033894e
EZ
391
392 /* If the text in clipboard is identical to what we put there
393 last time set_clipboard_data was called, pretend there's no
394 data in the clipboard. This is so we don't pass our own text
4029384b
EZ
395 from the clipboard (which might be troublesome if the killed
396 text includes null characters). */
5033894e
EZ
397 if (last_clipboard_text &&
398 xbuf_addr - xbuf_beg == (long)(lcdp - last_clipboard_text))
399 dp = (unsigned char *)Data + 1;
21cfcccf
EZ
400 }
401
402 free_xfer_buf ();
403
0265f89f 404 return (unsigned) (dp - (unsigned char *)Data - 1);
21cfcccf
EZ
405}
406
bbd240ce 407/* Close clipboard, return non-zero if successful. */
21cfcccf 408unsigned
3a8ce822 409close_clipboard (void)
21cfcccf
EZ
410{
411 __dpmi_regs regs;
412
413 /* Calls Int 2Fh/AX=1708h
414 Return Values AX == 0: Error occurred
415 <> 0: OK */
416 regs.x.ax = 0x1708;
5e617bc2 417 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
418 return regs.x.ax;
419}
420
421/* Compact clipboard data so that at least SIZE bytes is available. */
422unsigned
3a8ce822 423clipboard_compact (unsigned Size)
21cfcccf
EZ
424{
425 __dpmi_regs regs;
426
427 /* Calls Int 2Fh/AX=1709H with:
428 SI:CX = Desired memory size in bytes.
429 Return Values DX:AX == Number of bytes of largest block of free memory.
430 == 0 if error or no memory */
431 regs.x.ax = 0x1709;
432 regs.x.si = Size >> 16;
433 regs.x.cx = Size & 0xffff;
5e617bc2 434 __dpmi_int (0x2f, &regs);
21cfcccf
EZ
435 return ((unsigned)regs.x.dx << 16) | regs.x.ax;
436}
437\f
438static char no_mem_msg[] =
439 "(Not enough DOS memory to put saved text into clipboard.)";
0265f89f
EZ
440static char binary_msg[] =
441 "(Binary characters in saved text; clipboard data not set.)";
aae41d97
EZ
442static char system_error_msg[] =
443 "(Clipboard interface failure; clipboard data not set.)";
21cfcccf 444
767079a8 445DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_data, 1, 2, 0,
70da46c3 446 doc: /* This sets the clipboard data to the given text. */)
5842a27b 447 (Lisp_Object string, Lisp_Object frame)
21cfcccf 448{
0265f89f 449 unsigned ok = 1, put_status = 0;
891ef8f7 450 int nbytes, no_crlf_conversion;
583f5d53 451 unsigned char *src, *dst = NULL;
21cfcccf 452
b7826503 453 CHECK_STRING (string);
3276b4fe 454
21cfcccf
EZ
455 if (NILP (frame))
456 frame = Fselected_frame ();
457
b7826503 458 CHECK_LIVE_FRAME (frame);
21cfcccf
EZ
459 if ( !FRAME_MSDOS_P (XFRAME (frame)))
460 goto done;
3276b4fe 461
21cfcccf
EZ
462 BLOCK_INPUT;
463
1b912a3b
EZ
464 if (!open_clipboard ())
465 goto error;
466
d5db4077
KR
467 nbytes = SBYTES (string);
468 src = SDATA (string);
583f5d53 469
9b317797
EZ
470 /* Do we need to encode this text? */
471 for (dst = src; dst < src + nbytes; dst++)
472 {
473 if (*dst == '\0' || *dst >= 0x80)
474 break;
475 }
476 if (dst >= src + nbytes)
583f5d53 477 {
9b317797 478 /* No multibyte characters in text. We need not encode it, but we
583f5d53
EZ
479 will have to convert it to DOS CR-LF style. */
480 no_crlf_conversion = 0;
9b317797 481 Vlast_coding_system_used = Qraw_text;
1b912a3b 482 dst = NULL; /* so we don't try to free a random pointer */
583f5d53
EZ
483 }
484 else
485 {
486 /* We must encode contents of STRING according to what
487 clipboard-coding-system specifies. */
583f5d53 488 struct coding_system coding;
9b317797
EZ
489 Lisp_Object coding_system =
490 NILP (Vnext_selection_coding_system) ?
491 Vselection_coding_system : Vnext_selection_coding_system;
583f5d53 492
9b317797
EZ
493 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
494 coding.dst_bytes = nbytes * 4;
23f86fce 495 coding.destination = xmalloc (coding.dst_bytes);
9963e859 496 Vnext_selection_coding_system = Qnil;
583f5d53 497 coding.mode |= CODING_MODE_LAST_BLOCK;
9b317797
EZ
498 dst = coding.destination;
499 encode_coding_object (&coding, string, 0, 0,
500 SCHARS (string), nbytes, Qnil);
583f5d53 501 no_crlf_conversion = 1;
c7594627 502 nbytes = coding.produced;
9b317797 503 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
ba92a470 504 src = dst;
583f5d53
EZ
505 }
506
583f5d53 507 ok = empty_clipboard ()
0265f89f 508 && ((put_status
ba92a470 509 = set_clipboard_data (CF_OEMTEXT, src, nbytes, no_crlf_conversion))
0265f89f 510 == 0);
583f5d53
EZ
511
512 if (!no_crlf_conversion)
21cfcccf 513 close_clipboard ();
3276b4fe 514
31354c30 515 if (ok) goto unblock;
21cfcccf
EZ
516
517 error:
3276b4fe 518
21cfcccf
EZ
519 ok = 0;
520
31354c30 521 unblock:
70fdbb46 522 xfree (dst);
31354c30
EZ
523 UNBLOCK_INPUT;
524
21cfcccf
EZ
525 /* Notify user if the text is too large to fit into DOS memory.
526 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
527 depending on user system configuration.) If we just silently
528 fail the function, people might wonder why their text sometimes
529 doesn't make it to the clipboard. */
0265f89f 530 if (put_status)
21cfcccf 531 {
0265f89f
EZ
532 switch (put_status)
533 {
534 case 1:
535 message2 (no_mem_msg, sizeof (no_mem_msg) - 1, 0);
536 break;
537 case 2:
538 message2 (binary_msg, sizeof (binary_msg) - 1, 0);
539 break;
aae41d97
EZ
540 case 3:
541 message2 (system_error_msg, sizeof (system_error_msg) - 1, 0);
542 break;
0265f89f 543 }
b33e80aa 544 sit_for (make_number (2), 0, 2);
21cfcccf 545 }
3276b4fe 546
21cfcccf 547 done:
21cfcccf 548
0265f89f 549 return (ok && put_status == 0 ? string : Qnil);
21cfcccf
EZ
550}
551
767079a8 552DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_data, 0, 1, 0,
70da46c3 553 doc: /* This gets the clipboard data in text format. */)
5842a27b 554 (Lisp_Object frame)
21cfcccf
EZ
555{
556 unsigned data_size, truelen;
1b912a3b 557 unsigned char *htext = NULL;
21cfcccf 558 Lisp_Object ret = Qnil;
891ef8f7 559 int require_decoding = 0;
583f5d53 560
21cfcccf
EZ
561 if (NILP (frame))
562 frame = Fselected_frame ();
563
b7826503 564 CHECK_LIVE_FRAME (frame);
21cfcccf
EZ
565 if ( !FRAME_MSDOS_P (XFRAME (frame)))
566 goto done;
3276b4fe 567
21cfcccf 568 BLOCK_INPUT;
3276b4fe 569
21cfcccf 570 if (!open_clipboard ())
31354c30 571 goto unblock;
21cfcccf 572
538d05bb 573 if ((data_size = get_clipboard_data_size (CF_OEMTEXT)) == 0 ||
23f86fce 574 (htext = xmalloc (data_size)) == 0)
21cfcccf
EZ
575 goto closeclip;
576
577 /* need to know final size after '\r' chars are removed because
578 we can't change the string size manually, and doing an extra
579 copy is silly */
538d05bb 580 if ((truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 0)) == 0)
21cfcccf
EZ
581 goto closeclip;
582
583f5d53 583 /* Do we need to decode it? */
3276b4fe
EZ
584 {
585 /* If the clipboard data contains any 8-bit Latin-1 code, we
586 need to decode it. */
587 int i;
588
589 for (i = 0; i < truelen; i++)
590 {
591 if (htext[i] >= 0x80)
592 {
9b317797 593 require_decoding = 1;
3276b4fe
EZ
594 break;
595 }
596 }
597 }
9b317797 598 if (require_decoding)
583f5d53 599 {
583f5d53 600 struct coding_system coding;
9b317797 601 Lisp_Object coding_system = Vnext_selection_coding_system;
583f5d53 602
9b317797
EZ
603 truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 1);
604 if (NILP (coding_system))
605 coding_system = Vselection_coding_system;
606 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
607 coding.source = htext;
583f5d53 608 coding.mode |= CODING_MODE_LAST_BLOCK;
9b317797 609 /* We explicitly disable composition handling because selection
ee826e00 610 data should not contain any composition sequence. */
9b317797
EZ
611 coding.mode &= CODING_ANNOTATION_MASK;
612 decode_coding_object (&coding, Qnil, 0, 0, truelen, truelen, Qt);
613 ret = coding.dst_object;
614 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
583f5d53
EZ
615 }
616 else
617 {
618 ret = make_unibyte_string ((char *) htext, truelen);
619 Vlast_coding_system_used = Qraw_text;
620 }
621
21cfcccf 622 xfree (htext);
9b317797 623 Vnext_selection_coding_system = Qnil;
21cfcccf
EZ
624
625 closeclip:
626 close_clipboard ();
31354c30
EZ
627
628 unblock:
629 UNBLOCK_INPUT;
8de1855e 630
21cfcccf 631 done:
8de1855e 632
21cfcccf
EZ
633 return (ret);
634}
635
636/* Support checking for a clipboard selection. */
637
638DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
90b671e2
EZ
639 0, 2, 0,
640 doc: /* Whether there is an owner for the given X selection.
641SELECTION should be the name of the selection in question, typically
642one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
643these literal upper-case names.) The symbol nil is the same as
644`PRIMARY', and t is the same as `SECONDARY'.
645
646TERMINAL should be a terminal object or a frame specifying the X
647server to query. If omitted or nil, that stands for the selected
648frame's display, or the first available X display. */)
649 (Lisp_Object selection, Lisp_Object terminal)
21cfcccf 650{
b7826503 651 CHECK_SYMBOL (selection);
21cfcccf
EZ
652
653 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
654 selection, check if there is some text on the kill-ring;
655 for CLIPBOARD, check if the clipboard currently has valid
656 text format contents.
657
658 The test for killed text on the kill-ring emulates the Emacs
659 behavior on X, where killed text is also put into X selection
660 by the X interface code. (On MSDOS, killed text is only put
661 into the clipboard if we run under Windows, so we cannot check
662 the clipboard alone.) */
663 if ((EQ (selection, Qnil) || EQ (selection, QPRIMARY))
d16bdfc3
EZ
664 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
665 Qnil))))
21cfcccf
EZ
666 return Qt;
667
668 if (EQ (selection, QCLIPBOARD))
669 {
670 Lisp_Object val = Qnil;
671
672 if (open_clipboard ())
673 {
538d05bb 674 if (get_clipboard_data_size (CF_OEMTEXT))
21cfcccf
EZ
675 val = Qt;
676 close_clipboard ();
677 }
678 return val;
679 }
680 return Qnil;
681}
682
8de1855e 683void
3a8ce822 684syms_of_win16select (void)
21cfcccf 685{
767079a8
EZ
686 defsubr (&Sw16_set_clipboard_data);
687 defsubr (&Sw16_get_clipboard_data);
21cfcccf
EZ
688 defsubr (&Sx_selection_exists_p);
689
29208e82 690 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system,
f5f25615
GM
691 doc: /* Coding system for communicating with other programs.
692
693For MS-Windows and MS-DOS:
694When sending or receiving text via selection and clipboard, the text
695is encoded or decoded by this coding system. The default value is
696the current system default encoding on 9x/Me, `utf-16le-dos'
697\(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
698
699For X Windows:
700When sending text via selection and clipboard, if the target
701data-type matches with the type of this coding system, it is used
702for encoding the text. Otherwise (including the case that this
703variable is nil), a proper coding system is used as below:
704
705data-type coding system
706--------- -------------
707UTF8_STRING utf-8
708COMPOUND_TEXT compound-text-with-extensions
709STRING iso-latin-1
710C_STRING no-conversion
711
712When receiving text, if this coding system is non-nil, it is used
713for decoding regardless of the data-type. If this is nil, a
714proper coding system is used according to the data-type as above.
715
716See also the documentation of the variable `x-select-request-type' how
717to control which data-type to request for receiving text.
718
719The default value is nil. */);
b45b1e81 720 Vselection_coding_system = intern ("iso-latin-1-dos");
9963e859 721
29208e82 722 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system,
3646b86d 723 doc: /* Coding system for the next communication with other programs.
70da46c3 724Usually, `selection-coding-system' is used for communicating with
3646b86d
GM
725other programs (X Windows clients or MS Windows programs). But, if this
726variable is set, it is used for the next communication only.
727After the communication, this variable is set to nil. */);
9963e859 728 Vnext_selection_coding_system = Qnil;
583f5d53 729
21cfcccf
EZ
730 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
731 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
732}
733
734#endif /* MSDOS */