Commit | Line | Data |
---|---|---|
767079a8 | 1 | /* 16-bit Windows Selection processing for emacs on MS-Windows |
e9bffc61 | 2 | |
ba318903 | 3 | Copyright (C) 1996-1997, 2001-2014 Free Software Foundation, Inc. |
8de1855e | 4 | |
21cfcccf EZ |
5 | This file is part of GNU Emacs. |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
21cfcccf | 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. | |
21cfcccf EZ |
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/>. */ |
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 | ||
59 | unsigned identify_winoldap_version (void); | |
60 | unsigned open_clipboard (void); | |
61 | unsigned empty_clipboard (void); | |
583f5d53 | 62 | unsigned set_clipboard_data (unsigned, void *, unsigned, int); |
21cfcccf | 63 | unsigned get_clipboard_data_size (unsigned); |
583f5d53 | 64 | unsigned get_clipboard_data (unsigned, void *, unsigned, int); |
21cfcccf EZ |
65 | unsigned close_clipboard (void); |
66 | unsigned clipboard_compact (unsigned); | |
67 | ||
68 | Lisp_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 |
72 | static 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. */ | |
83 | static unsigned char *last_clipboard_text; | |
84 | ||
85 | /* The size of allocated storage for storing the clipboard data. */ | |
86 | static 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. */ | |
102 | unsigned | |
3a8ce822 | 103 | identify_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, ®s); |
21cfcccf EZ |
113 | return regs.x.ax; |
114 | } | |
115 | ||
bbd240ce | 116 | /* Open the clipboard, return non-zero if successful. */ |
21cfcccf | 117 | unsigned |
3a8ce822 | 118 | open_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, ®s); |
21cfcccf EZ |
136 | return regs.x.ax; |
137 | } | |
138 | ||
bbd240ce | 139 | /* Empty clipboard, return non-zero if successful. */ |
21cfcccf | 140 | unsigned |
3a8ce822 | 141 | empty_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, ®s); |
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. */ | |
155 | static unsigned long | |
3a8ce822 | 156 | alloc_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, ®s); | |
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. */ | |
192 | static void | |
3a8ce822 | 193 | free_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, ®s); | |
205 | clipboard_xfer_buf_info.size = 0; | |
206 | } | |
207 | } | |
208 | ||
bbd240ce | 209 | /* Copy data into the clipboard, return zero if successful. */ |
21cfcccf | 210 | unsigned |
3a8ce822 | 211 | set_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, ®s); |
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. */ | |
309 | unsigned | |
3a8ce822 | 310 | get_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, ®s); |
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. */ | |
329 | unsigned | |
3a8ce822 | 330 | get_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, ®s); |
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 | 408 | unsigned |
3a8ce822 | 409 | close_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, ®s); |
21cfcccf EZ |
418 | return regs.x.ax; |
419 | } | |
420 | ||
421 | /* Compact clipboard data so that at least SIZE bytes is available. */ | |
422 | unsigned | |
3a8ce822 | 423 | clipboard_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, ®s); |
21cfcccf EZ |
435 | return ((unsigned)regs.x.dx << 16) | regs.x.ax; |
436 | } | |
437 | \f | |
438 | static char no_mem_msg[] = | |
439 | "(Not enough DOS memory to put saved text into clipboard.)"; | |
0265f89f EZ |
440 | static char binary_msg[] = |
441 | "(Binary characters in saved text; clipboard data not set.)"; | |
aae41d97 EZ |
442 | static char system_error_msg[] = |
443 | "(Clipboard interface failure; clipboard data not set.)"; | |
21cfcccf | 444 | |
767079a8 | 445 | DEFUN ("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 | |
d2a95ffb | 455 | if (!FRAME_MSDOS_P (decode_live_frame (frame))) |
21cfcccf | 456 | goto done; |
3276b4fe | 457 | |
4d7e6e51 | 458 | block_input (); |
21cfcccf | 459 | |
1b912a3b EZ |
460 | if (!open_clipboard ()) |
461 | goto error; | |
462 | ||
d5db4077 KR |
463 | nbytes = SBYTES (string); |
464 | src = SDATA (string); | |
583f5d53 | 465 | |
9b317797 EZ |
466 | /* Do we need to encode this text? */ |
467 | for (dst = src; dst < src + nbytes; dst++) | |
468 | { | |
469 | if (*dst == '\0' || *dst >= 0x80) | |
470 | break; | |
471 | } | |
472 | if (dst >= src + nbytes) | |
583f5d53 | 473 | { |
9b317797 | 474 | /* No multibyte characters in text. We need not encode it, but we |
583f5d53 EZ |
475 | will have to convert it to DOS CR-LF style. */ |
476 | no_crlf_conversion = 0; | |
9b317797 | 477 | Vlast_coding_system_used = Qraw_text; |
1b912a3b | 478 | dst = NULL; /* so we don't try to free a random pointer */ |
583f5d53 EZ |
479 | } |
480 | else | |
481 | { | |
482 | /* We must encode contents of STRING according to what | |
483 | clipboard-coding-system specifies. */ | |
583f5d53 | 484 | struct coding_system coding; |
9b317797 EZ |
485 | Lisp_Object coding_system = |
486 | NILP (Vnext_selection_coding_system) ? | |
487 | Vselection_coding_system : Vnext_selection_coding_system; | |
583f5d53 | 488 | |
9b317797 EZ |
489 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
490 | coding.dst_bytes = nbytes * 4; | |
23f86fce | 491 | coding.destination = xmalloc (coding.dst_bytes); |
9963e859 | 492 | Vnext_selection_coding_system = Qnil; |
583f5d53 | 493 | coding.mode |= CODING_MODE_LAST_BLOCK; |
9b317797 EZ |
494 | dst = coding.destination; |
495 | encode_coding_object (&coding, string, 0, 0, | |
496 | SCHARS (string), nbytes, Qnil); | |
583f5d53 | 497 | no_crlf_conversion = 1; |
c7594627 | 498 | nbytes = coding.produced; |
9b317797 | 499 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); |
ba92a470 | 500 | src = dst; |
583f5d53 EZ |
501 | } |
502 | ||
583f5d53 | 503 | ok = empty_clipboard () |
0265f89f | 504 | && ((put_status |
ba92a470 | 505 | = set_clipboard_data (CF_OEMTEXT, src, nbytes, no_crlf_conversion)) |
0265f89f | 506 | == 0); |
583f5d53 EZ |
507 | |
508 | if (!no_crlf_conversion) | |
21cfcccf | 509 | close_clipboard (); |
3276b4fe | 510 | |
31354c30 | 511 | if (ok) goto unblock; |
21cfcccf EZ |
512 | |
513 | error: | |
3276b4fe | 514 | |
21cfcccf EZ |
515 | ok = 0; |
516 | ||
31354c30 | 517 | unblock: |
70fdbb46 | 518 | xfree (dst); |
4d7e6e51 | 519 | unblock_input (); |
31354c30 | 520 | |
21cfcccf EZ |
521 | /* Notify user if the text is too large to fit into DOS memory. |
522 | (This will happen somewhere after 600K bytes (470K in DJGPP v1.x), | |
523 | depending on user system configuration.) If we just silently | |
524 | fail the function, people might wonder why their text sometimes | |
525 | doesn't make it to the clipboard. */ | |
0265f89f | 526 | if (put_status) |
21cfcccf | 527 | { |
0265f89f EZ |
528 | switch (put_status) |
529 | { | |
530 | case 1: | |
b09cca6a | 531 | message3 (make_unibyte_string (no_mem_msg, sizeof (no_mem_msg) - 1)); |
0265f89f EZ |
532 | break; |
533 | case 2: | |
b09cca6a | 534 | message3 (make_unibyte_string (binary_msg, sizeof (binary_msg) - 1)); |
0265f89f | 535 | break; |
aae41d97 | 536 | case 3: |
b09cca6a | 537 | message3 (make_unibyte_string (system_error_msg, sizeof (system_error_msg) - 1)); |
aae41d97 | 538 | break; |
0265f89f | 539 | } |
b33e80aa | 540 | sit_for (make_number (2), 0, 2); |
21cfcccf | 541 | } |
3276b4fe | 542 | |
21cfcccf | 543 | done: |
21cfcccf | 544 | |
0265f89f | 545 | return (ok && put_status == 0 ? string : Qnil); |
21cfcccf EZ |
546 | } |
547 | ||
767079a8 | 548 | DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_data, 0, 1, 0, |
70da46c3 | 549 | doc: /* This gets the clipboard data in text format. */) |
5842a27b | 550 | (Lisp_Object frame) |
21cfcccf EZ |
551 | { |
552 | unsigned data_size, truelen; | |
1b912a3b | 553 | unsigned char *htext = NULL; |
21cfcccf | 554 | Lisp_Object ret = Qnil; |
891ef8f7 | 555 | int require_decoding = 0; |
583f5d53 | 556 | |
d2a95ffb | 557 | if (!FRAME_MSDOS_P (decode_live_frame (frame))) |
21cfcccf | 558 | goto done; |
3276b4fe | 559 | |
4d7e6e51 | 560 | block_input (); |
3276b4fe | 561 | |
21cfcccf | 562 | if (!open_clipboard ()) |
31354c30 | 563 | goto unblock; |
21cfcccf | 564 | |
538d05bb | 565 | if ((data_size = get_clipboard_data_size (CF_OEMTEXT)) == 0 || |
23f86fce | 566 | (htext = xmalloc (data_size)) == 0) |
21cfcccf EZ |
567 | goto closeclip; |
568 | ||
569 | /* need to know final size after '\r' chars are removed because | |
570 | we can't change the string size manually, and doing an extra | |
571 | copy is silly */ | |
538d05bb | 572 | if ((truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 0)) == 0) |
21cfcccf EZ |
573 | goto closeclip; |
574 | ||
583f5d53 | 575 | /* Do we need to decode it? */ |
3276b4fe EZ |
576 | { |
577 | /* If the clipboard data contains any 8-bit Latin-1 code, we | |
578 | need to decode it. */ | |
579 | int i; | |
580 | ||
581 | for (i = 0; i < truelen; i++) | |
582 | { | |
583 | if (htext[i] >= 0x80) | |
584 | { | |
9b317797 | 585 | require_decoding = 1; |
3276b4fe EZ |
586 | break; |
587 | } | |
588 | } | |
589 | } | |
9b317797 | 590 | if (require_decoding) |
583f5d53 | 591 | { |
583f5d53 | 592 | struct coding_system coding; |
9b317797 | 593 | Lisp_Object coding_system = Vnext_selection_coding_system; |
583f5d53 | 594 | |
9b317797 EZ |
595 | truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 1); |
596 | if (NILP (coding_system)) | |
597 | coding_system = Vselection_coding_system; | |
598 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); | |
599 | coding.source = htext; | |
583f5d53 | 600 | coding.mode |= CODING_MODE_LAST_BLOCK; |
9b317797 | 601 | /* We explicitly disable composition handling because selection |
ee826e00 | 602 | data should not contain any composition sequence. */ |
9b317797 EZ |
603 | coding.mode &= CODING_ANNOTATION_MASK; |
604 | decode_coding_object (&coding, Qnil, 0, 0, truelen, truelen, Qt); | |
605 | ret = coding.dst_object; | |
606 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
583f5d53 EZ |
607 | } |
608 | else | |
609 | { | |
610 | ret = make_unibyte_string ((char *) htext, truelen); | |
611 | Vlast_coding_system_used = Qraw_text; | |
612 | } | |
613 | ||
21cfcccf | 614 | xfree (htext); |
9b317797 | 615 | Vnext_selection_coding_system = Qnil; |
21cfcccf EZ |
616 | |
617 | closeclip: | |
618 | close_clipboard (); | |
31354c30 EZ |
619 | |
620 | unblock: | |
4d7e6e51 | 621 | unblock_input (); |
8de1855e | 622 | |
21cfcccf | 623 | done: |
8de1855e | 624 | |
21cfcccf EZ |
625 | return (ret); |
626 | } | |
627 | ||
628 | /* Support checking for a clipboard selection. */ | |
629 | ||
630 | DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, | |
90b671e2 EZ |
631 | 0, 2, 0, |
632 | doc: /* Whether there is an owner for the given X selection. | |
633 | SELECTION should be the name of the selection in question, typically | |
634 | one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects | |
635 | these literal upper-case names.) The symbol nil is the same as | |
636 | `PRIMARY', and t is the same as `SECONDARY'. | |
637 | ||
638 | TERMINAL should be a terminal object or a frame specifying the X | |
639 | server to query. If omitted or nil, that stands for the selected | |
640 | frame's display, or the first available X display. */) | |
641 | (Lisp_Object selection, Lisp_Object terminal) | |
21cfcccf | 642 | { |
b7826503 | 643 | CHECK_SYMBOL (selection); |
21cfcccf EZ |
644 | |
645 | /* Return nil for SECONDARY selection. For PRIMARY (or nil) | |
646 | selection, check if there is some text on the kill-ring; | |
647 | for CLIPBOARD, check if the clipboard currently has valid | |
648 | text format contents. | |
649 | ||
650 | The test for killed text on the kill-ring emulates the Emacs | |
651 | behavior on X, where killed text is also put into X selection | |
652 | by the X interface code. (On MSDOS, killed text is only put | |
653 | into the clipboard if we run under Windows, so we cannot check | |
654 | the clipboard alone.) */ | |
655 | if ((EQ (selection, Qnil) || EQ (selection, QPRIMARY)) | |
d16bdfc3 EZ |
656 | && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"), |
657 | Qnil)))) | |
21cfcccf EZ |
658 | return Qt; |
659 | ||
660 | if (EQ (selection, QCLIPBOARD)) | |
661 | { | |
662 | Lisp_Object val = Qnil; | |
663 | ||
664 | if (open_clipboard ()) | |
665 | { | |
538d05bb | 666 | if (get_clipboard_data_size (CF_OEMTEXT)) |
21cfcccf EZ |
667 | val = Qt; |
668 | close_clipboard (); | |
669 | } | |
670 | return val; | |
671 | } | |
672 | return Qnil; | |
673 | } | |
674 | ||
8de1855e | 675 | void |
3a8ce822 | 676 | syms_of_win16select (void) |
21cfcccf | 677 | { |
767079a8 EZ |
678 | defsubr (&Sw16_set_clipboard_data); |
679 | defsubr (&Sw16_get_clipboard_data); | |
21cfcccf EZ |
680 | defsubr (&Sx_selection_exists_p); |
681 | ||
29208e82 | 682 | DEFVAR_LISP ("selection-coding-system", Vselection_coding_system, |
f5f25615 GM |
683 | doc: /* Coding system for communicating with other programs. |
684 | ||
685 | For MS-Windows and MS-DOS: | |
686 | When sending or receiving text via selection and clipboard, the text | |
687 | is encoded or decoded by this coding system. The default value is | |
688 | the current system default encoding on 9x/Me, `utf-16le-dos' | |
689 | \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS. | |
690 | ||
691 | For X Windows: | |
692 | When sending text via selection and clipboard, if the target | |
693 | data-type matches with the type of this coding system, it is used | |
694 | for encoding the text. Otherwise (including the case that this | |
695 | variable is nil), a proper coding system is used as below: | |
696 | ||
697 | data-type coding system | |
698 | --------- ------------- | |
699 | UTF8_STRING utf-8 | |
700 | COMPOUND_TEXT compound-text-with-extensions | |
701 | STRING iso-latin-1 | |
702 | C_STRING no-conversion | |
703 | ||
704 | When receiving text, if this coding system is non-nil, it is used | |
705 | for decoding regardless of the data-type. If this is nil, a | |
706 | proper coding system is used according to the data-type as above. | |
707 | ||
708 | See also the documentation of the variable `x-select-request-type' how | |
709 | to control which data-type to request for receiving text. | |
710 | ||
711 | The default value is nil. */); | |
b45b1e81 | 712 | Vselection_coding_system = intern ("iso-latin-1-dos"); |
9963e859 | 713 | |
29208e82 | 714 | DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system, |
3646b86d | 715 | doc: /* Coding system for the next communication with other programs. |
70da46c3 | 716 | Usually, `selection-coding-system' is used for communicating with |
3646b86d GM |
717 | other programs (X Windows clients or MS Windows programs). But, if this |
718 | variable is set, it is used for the next communication only. | |
719 | After the communication, this variable is set to nil. */); | |
9963e859 | 720 | Vnext_selection_coding_system = Qnil; |
583f5d53 | 721 | |
21cfcccf EZ |
722 | QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY); |
723 | QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD); | |
724 | } | |
725 | ||
726 | #endif /* MSDOS */ |