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