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