| 1 | /* 16-bit Windows Selection processing for emacs on MS-Windows |
| 2 | |
| 3 | Copyright (C) 1996-1997, 2001-2011 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 7 | GNU Emacs is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 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 |
| 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 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> */ |
| 26 | /* Adapted to DJGPP by Eli Zaretskii <eliz@gnu.org> */ |
| 27 | |
| 28 | #ifdef MSDOS |
| 29 | |
| 30 | #include <config.h> |
| 31 | #include <dpmi.h> |
| 32 | #include <go32.h> |
| 33 | #include <sys/farptr.h> |
| 34 | #include <setjmp.h> |
| 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" |
| 39 | #include "buffer.h" |
| 40 | #include "character.h" |
| 41 | #include "coding.h" |
| 42 | #include "composite.h" |
| 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 | |
| 60 | unsigned identify_winoldap_version (void); |
| 61 | unsigned open_clipboard (void); |
| 62 | unsigned empty_clipboard (void); |
| 63 | unsigned set_clipboard_data (unsigned, void *, unsigned, int); |
| 64 | unsigned get_clipboard_data_size (unsigned); |
| 65 | unsigned get_clipboard_data (unsigned, void *, unsigned, int); |
| 66 | unsigned close_clipboard (void); |
| 67 | unsigned clipboard_compact (unsigned); |
| 68 | |
| 69 | Lisp_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. */ |
| 73 | static struct { |
| 74 | unsigned long size; |
| 75 | unsigned short rm_segment; |
| 76 | } clipboard_xfer_buf_info; |
| 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. */ |
| 84 | static unsigned char *last_clipboard_text; |
| 85 | |
| 86 | /* The size of allocated storage for storing the clipboard data. */ |
| 87 | static size_t clipboard_storage_size; |
| 88 | \f |
| 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. */ |
| 103 | unsigned |
| 104 | identify_winoldap_version (void) |
| 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; |
| 113 | __dpmi_int(0x2f, ®s); |
| 114 | return regs.x.ax; |
| 115 | } |
| 116 | |
| 117 | /* Open the clipboard, return non-zero if successfull. */ |
| 118 | unsigned |
| 119 | open_clipboard (void) |
| 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; |
| 136 | __dpmi_int(0x2f, ®s); |
| 137 | return regs.x.ax; |
| 138 | } |
| 139 | |
| 140 | /* Empty clipboard, return non-zero if successfull. */ |
| 141 | unsigned |
| 142 | empty_clipboard (void) |
| 143 | { |
| 144 | __dpmi_regs regs; |
| 145 | |
| 146 | /* Calls Int 2Fh/AX=1702h |
| 147 | Return Values AX == 0: Error occurred |
| 148 | <> 0: OK, Clipboard emptied */ |
| 149 | regs.x.ax = 0x1702; |
| 150 | __dpmi_int(0x2f, ®s); |
| 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. */ |
| 156 | static unsigned long |
| 157 | alloc_xfer_buf (unsigned want_size) |
| 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 | |
| 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 | |
| 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, ®s); |
| 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. */ |
| 193 | static void |
| 194 | free_xfer_buf (void) |
| 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, ®s); |
| 206 | clipboard_xfer_buf_info.size = 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* Copy data into the clipboard, return zero if successfull. */ |
| 211 | unsigned |
| 212 | set_clipboard_data (unsigned Format, void *Data, unsigned Size, int Raw) |
| 213 | { |
| 214 | __dpmi_regs regs; |
| 215 | unsigned truelen; |
| 216 | unsigned long xbuf_addr, buf_offset; |
| 217 | unsigned char *dp = Data, *dstart = dp; |
| 218 | |
| 219 | if (Format != CF_OEMTEXT) |
| 220 | return 3; |
| 221 | |
| 222 | /* need to know final size after '\r' chars are inserted (the |
| 223 | standard CF_OEMTEXT clipboard format uses CRLF line endings, |
| 224 | while Emacs uses just LF internally). */ |
| 225 | truelen = Size + 1; /* +1 for the terminating null */ |
| 226 | |
| 227 | if (!Raw) |
| 228 | { |
| 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 | } |
| 235 | } |
| 236 | |
| 237 | if (clipboard_compact (truelen) < truelen) |
| 238 | return 1; |
| 239 | |
| 240 | if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0) |
| 241 | return 1; |
| 242 | |
| 243 | /* Move the buffer into the low memory, convert LF into CR-LF if needed. */ |
| 244 | if (Raw) |
| 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 | } |
| 252 | else |
| 253 | { |
| 254 | dp = Data; |
| 255 | buf_offset = xbuf_addr; |
| 256 | _farsetsel (_dos_ds); |
| 257 | while (Size--) |
| 258 | { |
| 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; |
| 263 | if (*dp == '\n') |
| 264 | _farnspokeb (buf_offset++, '\r'); |
| 265 | _farnspokeb (buf_offset++, *dp++); |
| 266 | } |
| 267 | |
| 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 | } |
| 272 | |
| 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 | |
| 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; |
| 297 | __dpmi_int(0x2f, ®s); |
| 298 | |
| 299 | free_xfer_buf (); |
| 300 | |
| 301 | /* If the above failed, invalidate the local copy of the clipboard. */ |
| 302 | if (regs.x.ax == 0) |
| 303 | *last_clipboard_text = '\0'; |
| 304 | |
| 305 | /* Zero means success, otherwise (1, 2, or 3) it's an error. */ |
| 306 | return regs.x.ax > 0 ? 0 : 3; |
| 307 | } |
| 308 | |
| 309 | /* Return the size of the clipboard data of format FORMAT. */ |
| 310 | unsigned |
| 311 | get_clipboard_data_size (unsigned Format) |
| 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; |
| 323 | __dpmi_int(0x2f, ®s); |
| 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. */ |
| 330 | unsigned |
| 331 | get_clipboard_data (unsigned Format, void *Data, unsigned Size, int Raw) |
| 332 | { |
| 333 | __dpmi_regs regs; |
| 334 | unsigned long xbuf_addr; |
| 335 | unsigned char *dp = Data; |
| 336 | |
| 337 | if (Format != CF_OEMTEXT) |
| 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; |
| 356 | __dpmi_int(0x2f, ®s); |
| 357 | if (regs.x.ax != 0) |
| 358 | { |
| 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; |
| 366 | |
| 367 | /* Copy data from low memory, remove CR |
| 368 | characters before LF if needed. */ |
| 369 | _farsetsel (_dos_ds); |
| 370 | while (Size--) |
| 371 | { |
| 372 | register unsigned char c = _farnspeekb (xbuf_addr++); |
| 373 | |
| 374 | if (*lcdp == c) |
| 375 | lcdp++; |
| 376 | |
| 377 | if ((*dp++ = c) == '\r' && !Raw && _farnspeekb (xbuf_addr) == '\n') |
| 378 | { |
| 379 | dp--; |
| 380 | *dp++ = '\n'; |
| 381 | xbuf_addr++; |
| 382 | if (*lcdp == '\n') |
| 383 | lcdp++; |
| 384 | } |
| 385 | /* Windows reportedly rounds up the size of clipboard data |
| 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') |
| 390 | break; |
| 391 | } |
| 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 |
| 396 | from the clipboard (which might be troublesome if the killed |
| 397 | text includes null characters). */ |
| 398 | if (last_clipboard_text && |
| 399 | xbuf_addr - xbuf_beg == (long)(lcdp - last_clipboard_text)) |
| 400 | dp = (unsigned char *)Data + 1; |
| 401 | } |
| 402 | |
| 403 | free_xfer_buf (); |
| 404 | |
| 405 | return (unsigned) (dp - (unsigned char *)Data - 1); |
| 406 | } |
| 407 | |
| 408 | /* Close clipboard, return non-zero if successfull. */ |
| 409 | unsigned |
| 410 | close_clipboard (void) |
| 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; |
| 418 | __dpmi_int(0x2f, ®s); |
| 419 | return regs.x.ax; |
| 420 | } |
| 421 | |
| 422 | /* Compact clipboard data so that at least SIZE bytes is available. */ |
| 423 | unsigned |
| 424 | clipboard_compact (unsigned Size) |
| 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; |
| 435 | __dpmi_int(0x2f, ®s); |
| 436 | return ((unsigned)regs.x.dx << 16) | regs.x.ax; |
| 437 | } |
| 438 | \f |
| 439 | static char no_mem_msg[] = |
| 440 | "(Not enough DOS memory to put saved text into clipboard.)"; |
| 441 | static char binary_msg[] = |
| 442 | "(Binary characters in saved text; clipboard data not set.)"; |
| 443 | static char system_error_msg[] = |
| 444 | "(Clipboard interface failure; clipboard data not set.)"; |
| 445 | |
| 446 | DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_data, 1, 2, 0, |
| 447 | doc: /* This sets the clipboard data to the given text. */) |
| 448 | (Lisp_Object string, Lisp_Object frame) |
| 449 | { |
| 450 | unsigned ok = 1, put_status = 0; |
| 451 | int nbytes, no_crlf_conversion; |
| 452 | unsigned char *src, *dst = NULL; |
| 453 | |
| 454 | CHECK_STRING (string); |
| 455 | |
| 456 | if (NILP (frame)) |
| 457 | frame = Fselected_frame (); |
| 458 | |
| 459 | CHECK_LIVE_FRAME (frame); |
| 460 | if ( !FRAME_MSDOS_P (XFRAME (frame))) |
| 461 | goto done; |
| 462 | |
| 463 | BLOCK_INPUT; |
| 464 | |
| 465 | if (!open_clipboard ()) |
| 466 | goto error; |
| 467 | |
| 468 | nbytes = SBYTES (string); |
| 469 | src = SDATA (string); |
| 470 | |
| 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) |
| 478 | { |
| 479 | /* No multibyte characters in text. We need not encode it, but we |
| 480 | will have to convert it to DOS CR-LF style. */ |
| 481 | no_crlf_conversion = 0; |
| 482 | Vlast_coding_system_used = Qraw_text; |
| 483 | dst = NULL; /* so we don't try to free a random pointer */ |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | /* We must encode contents of STRING according to what |
| 488 | clipboard-coding-system specifies. */ |
| 489 | struct coding_system coding; |
| 490 | Lisp_Object coding_system = |
| 491 | NILP (Vnext_selection_coding_system) ? |
| 492 | Vselection_coding_system : Vnext_selection_coding_system; |
| 493 | |
| 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); |
| 497 | Vnext_selection_coding_system = Qnil; |
| 498 | coding.mode |= CODING_MODE_LAST_BLOCK; |
| 499 | dst = coding.destination; |
| 500 | encode_coding_object (&coding, string, 0, 0, |
| 501 | SCHARS (string), nbytes, Qnil); |
| 502 | no_crlf_conversion = 1; |
| 503 | nbytes = coding.produced; |
| 504 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); |
| 505 | src = dst; |
| 506 | } |
| 507 | |
| 508 | ok = empty_clipboard () |
| 509 | && ((put_status |
| 510 | = set_clipboard_data (CF_OEMTEXT, src, nbytes, no_crlf_conversion)) |
| 511 | == 0); |
| 512 | |
| 513 | if (!no_crlf_conversion) |
| 514 | close_clipboard (); |
| 515 | |
| 516 | if (ok) goto unblock; |
| 517 | |
| 518 | error: |
| 519 | |
| 520 | ok = 0; |
| 521 | |
| 522 | unblock: |
| 523 | xfree (dst); |
| 524 | UNBLOCK_INPUT; |
| 525 | |
| 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. */ |
| 531 | if (put_status) |
| 532 | { |
| 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; |
| 541 | case 3: |
| 542 | message2 (system_error_msg, sizeof (system_error_msg) - 1, 0); |
| 543 | break; |
| 544 | } |
| 545 | sit_for (make_number (2), 0, 2); |
| 546 | } |
| 547 | |
| 548 | done: |
| 549 | |
| 550 | return (ok && put_status == 0 ? string : Qnil); |
| 551 | } |
| 552 | |
| 553 | DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_data, 0, 1, 0, |
| 554 | doc: /* This gets the clipboard data in text format. */) |
| 555 | (Lisp_Object frame) |
| 556 | { |
| 557 | unsigned data_size, truelen; |
| 558 | unsigned char *htext = NULL; |
| 559 | Lisp_Object ret = Qnil; |
| 560 | int require_decoding = 0; |
| 561 | |
| 562 | if (NILP (frame)) |
| 563 | frame = Fselected_frame (); |
| 564 | |
| 565 | CHECK_LIVE_FRAME (frame); |
| 566 | if ( !FRAME_MSDOS_P (XFRAME (frame))) |
| 567 | goto done; |
| 568 | |
| 569 | BLOCK_INPUT; |
| 570 | |
| 571 | if (!open_clipboard ()) |
| 572 | goto unblock; |
| 573 | |
| 574 | if ((data_size = get_clipboard_data_size (CF_OEMTEXT)) == 0 || |
| 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 */ |
| 581 | if ((truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 0)) == 0) |
| 582 | goto closeclip; |
| 583 | |
| 584 | /* Do we need to decode it? */ |
| 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 | { |
| 594 | require_decoding = 1; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | if (require_decoding) |
| 600 | { |
| 601 | struct coding_system coding; |
| 602 | Lisp_Object coding_system = Vnext_selection_coding_system; |
| 603 | |
| 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; |
| 609 | coding.mode |= CODING_MODE_LAST_BLOCK; |
| 610 | /* We explicitly disable composition handling because selection |
| 611 | data should not contain any composition sequence. */ |
| 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); |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | ret = make_unibyte_string ((char *) htext, truelen); |
| 620 | Vlast_coding_system_used = Qraw_text; |
| 621 | } |
| 622 | |
| 623 | xfree (htext); |
| 624 | Vnext_selection_coding_system = Qnil; |
| 625 | |
| 626 | closeclip: |
| 627 | close_clipboard (); |
| 628 | |
| 629 | unblock: |
| 630 | UNBLOCK_INPUT; |
| 631 | |
| 632 | done: |
| 633 | |
| 634 | return (ret); |
| 635 | } |
| 636 | |
| 637 | /* Support checking for a clipboard selection. */ |
| 638 | |
| 639 | DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, |
| 640 | 0, 1, 0, |
| 641 | doc: /* Whether there is an owner for the given X Selection. |
| 642 | The arg should be the name of the selection in question, typically one of |
| 643 | the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. |
| 644 | \(Those are literal upper-case symbol names, since that's what X expects.) |
| 645 | For convenience, the symbol nil is the same as `PRIMARY', |
| 646 | and t is the same as `SECONDARY'. */) |
| 647 | (Lisp_Object selection) |
| 648 | { |
| 649 | CHECK_SYMBOL (selection); |
| 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)) |
| 662 | && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"), |
| 663 | Qnil)))) |
| 664 | return Qt; |
| 665 | |
| 666 | if (EQ (selection, QCLIPBOARD)) |
| 667 | { |
| 668 | Lisp_Object val = Qnil; |
| 669 | |
| 670 | if (open_clipboard ()) |
| 671 | { |
| 672 | if (get_clipboard_data_size (CF_OEMTEXT)) |
| 673 | val = Qt; |
| 674 | close_clipboard (); |
| 675 | } |
| 676 | return val; |
| 677 | } |
| 678 | return Qnil; |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | syms_of_win16select (void) |
| 683 | { |
| 684 | defsubr (&Sw16_set_clipboard_data); |
| 685 | defsubr (&Sw16_get_clipboard_data); |
| 686 | defsubr (&Sx_selection_exists_p); |
| 687 | |
| 688 | DEFVAR_LISP ("selection-coding-system", Vselection_coding_system, |
| 689 | doc: /* Coding system for communicating with other programs. |
| 690 | |
| 691 | For MS-Windows and MS-DOS: |
| 692 | When sending or receiving text via selection and clipboard, the text |
| 693 | is encoded or decoded by this coding system. The default value is |
| 694 | the 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 | |
| 697 | For X Windows: |
| 698 | When sending text via selection and clipboard, if the target |
| 699 | data-type matches with the type of this coding system, it is used |
| 700 | for encoding the text. Otherwise (including the case that this |
| 701 | variable is nil), a proper coding system is used as below: |
| 702 | |
| 703 | data-type coding system |
| 704 | --------- ------------- |
| 705 | UTF8_STRING utf-8 |
| 706 | COMPOUND_TEXT compound-text-with-extensions |
| 707 | STRING iso-latin-1 |
| 708 | C_STRING no-conversion |
| 709 | |
| 710 | When receiving text, if this coding system is non-nil, it is used |
| 711 | for decoding regardless of the data-type. If this is nil, a |
| 712 | proper coding system is used according to the data-type as above. |
| 713 | |
| 714 | See also the documentation of the variable `x-select-request-type' how |
| 715 | to control which data-type to request for receiving text. |
| 716 | |
| 717 | The default value is nil. */); |
| 718 | Vselection_coding_system = intern ("iso-latin-1-dos"); |
| 719 | |
| 720 | DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system, |
| 721 | doc: /* Coding system for the next communication with other programs. |
| 722 | Usually, `selection-coding-system' is used for communicating with |
| 723 | other programs (X Windows clients or MS Windows programs). But, if this |
| 724 | variable is set, it is used for the next communication only. |
| 725 | After the communication, this variable is set to nil. */); |
| 726 | Vnext_selection_coding_system = Qnil; |
| 727 | |
| 728 | QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY); |
| 729 | QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD); |
| 730 | } |
| 731 | |
| 732 | #endif /* MSDOS */ |
| 733 | |