X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/0236e3f15175a263f3c768d7a8431c7b80248aab..490468dcffd66ab9ea64bc1204ac9692dc530e8c:/src/w16select.c diff --git a/src/w16select.c b/src/w16select.c index e94a420d1a..df739a1356 100644 --- a/src/w16select.c +++ b/src/w16select.c @@ -77,11 +77,21 @@ static Lisp_Object Vnext_selection_coding_system; /* The segment address and the size of the buffer in low memory used to move data between us and WinOldAp module. */ - static struct { unsigned long size; unsigned short rm_segment; } clipboard_xfer_buf_info; + +/* The last text we put into the clipboard. This is used to prevent + passing back our own text from the clipboard, instead of using the + kill ring. The former is undesirable because the clipboard data + could be MULEtilated by inappropriately chosen + (next-)selection-coding-system. For this reason, we must store the + text *after* it was encoded/Unix-to-DOS-converted. */ +static unsigned char *last_clipboard_text; + +/* The size of allocated storage for storing the clipboard data. */ +static size_t clipboard_storage_size; /* Emulation of `__dpmi_int' and friends for DJGPP v1.x */ @@ -238,7 +248,7 @@ set_clipboard_data (Format, Data, Size, Raw) unsigned char *dp = Data, *dstart = dp; if (Format != CF_OEMTEXT) - return 0; + return 3; /* need to know final size after '\r' chars are inserted (the standard CF_OEMTEXT clipboard format uses CRLF line endings, @@ -256,10 +266,10 @@ set_clipboard_data (Format, Data, Size, Raw) } if (clipboard_compact (truelen) < truelen) - return 0; + return 1; if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0) - return 0; + return 1; /* Move the buffer into the low memory, convert LF into CR-LF if needed. */ if (Raw) @@ -291,6 +301,18 @@ set_clipboard_data (Format, Data, Size, Raw) _farnspokeb (buf_offset, '\0'); } + /* Stash away the data we are about to put into the clipboard, so we + could later check inside get_clipboard_data whether the clipboard + still holds our data. */ + if (clipboard_storage_size < truelen) + { + clipboard_storage_size = truelen + 100; + last_clipboard_text = + (char *) xrealloc (last_clipboard_text, clipboard_storage_size); + } + if (last_clipboard_text) + dosmemget (xbuf_addr, truelen, last_clipboard_text); + /* Calls Int 2Fh/AX=1703h with: DX = WinOldAp-Supported Clipboard format ES:BX = Pointer to data @@ -307,8 +329,12 @@ set_clipboard_data (Format, Data, Size, Raw) free_xfer_buf (); - /* Zero means success, otherwise (1 or 2) it's an error. */ - return regs.x.ax > 0 ? 0 : 1; + /* If the above failed, invalidate the local copy of the clipboard. */ + if (regs.x.ax == 0) + *last_clipboard_text = '\0'; + + /* Zero means success, otherwise (1, 2, or 3) it's an error. */ + return regs.x.ax > 0 ? 0 : 3; } /* Return the size of the clipboard data of format FORMAT. */ @@ -343,10 +369,6 @@ get_clipboard_data (Format, Data, Size, Raw) __dpmi_regs regs; unsigned long xbuf_addr; unsigned char *dp = Data; - /* The last 32-byte aligned block of data. See commentary below. */ - unsigned char *last_block = dp + ((Size & 0x1f) - ? (Size & 0x20) - : Size - 0x20); if (Format != CF_OEMTEXT) return 0; @@ -370,6 +392,14 @@ get_clipboard_data (Format, Data, Size, Raw) __dpmi_int(0x2f, ®s); if (regs.x.ax != 0) { + unsigned char null_char = '\0'; + unsigned long xbuf_beg = xbuf_addr; + + /* If last_clipboard_text is NULL, we don't want to slow down + the next loop by an additional test. */ + register unsigned char *lcdp = + last_clipboard_text == NULL ? &null_char : last_clipboard_text; + /* Copy data from low memory, remove CR characters before LF if needed. */ _farsetsel (_dos_ds); @@ -377,20 +407,33 @@ get_clipboard_data (Format, Data, Size, Raw) { register unsigned char c = _farnspeekb (xbuf_addr++); + if (*lcdp == c) + lcdp++; + if ((*dp++ = c) == '\r' && !Raw && _farnspeekb (xbuf_addr) == '\n') { dp--; *dp++ = '\n'; xbuf_addr++; - last_block--; /* adjust the beginning of the last 32 bytes */ + if (*lcdp == '\n') + lcdp++; } /* Windows reportedly rounds up the size of clipboard data - (passed in SIZE) to a multiple of 32. We therefore bail - out when we see the first null character in the last 32-byte - block. */ - else if (c == '\0' && dp > last_block) + (passed in SIZE) to a multiple of 32, and removes trailing + spaces from each line without updating SIZE. We therefore + bail out when we see the first null character. */ + else if (c == '\0') break; } + + /* If the text in clipboard is identical to what we put there + last time set_clipboard_data was called, pretend there's no + data in the clipboard. This is so we don't pass our own text + from the clipboard (which might be troublesome if the killed + text includes null characters). */ + if (last_clipboard_text && + xbuf_addr - xbuf_beg == (long)(lcdp - last_clipboard_text)) + dp = (unsigned char *)Data + 1; } free_xfer_buf (); @@ -434,6 +477,8 @@ static char no_mem_msg[] = "(Not enough DOS memory to put saved text into clipboard.)"; static char binary_msg[] = "(Binary characters in saved text; clipboard data not set.)"; +static char system_error_msg[] = + "(Clipboard interface failure; clipboard data not set.)"; DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_data, 1, 2, 0, "This sets the clipboard data to the given text.") @@ -443,8 +488,7 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat unsigned ok = 1, put_status = 0; int nbytes; unsigned char *src, *dst = NULL; - int charsets[MAX_CHARSET + 1]; - int num; + int charset_info; int no_crlf_conversion; CHECK_STRING (string, 0); @@ -463,14 +507,10 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat /* Since we are now handling multilingual text, we must consider encoding text for the clipboard. */ - bzero (charsets, (MAX_CHARSET + 1) * sizeof (int)); - num = ((nbytes <= 1 /* Check the possibility of short cut. */ - || !STRING_MULTIBYTE (string) - || nbytes == XSTRING (string)->size) - ? 0 - : find_charset_in_str (src, nbytes, charsets, Qnil, 0, 1)); - - if (!num || (num == 1 && charsets[CHARSET_ASCII])) + charset_info = find_charset_in_text (src, XSTRING (string)->size, nbytes, + NULL, Qnil); + + if (charset_info == 0) { /* No multibyte character in OBJ. We need not encode it, but we will have to convert it to DOS CR-LF style. */ @@ -488,6 +528,8 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat Vnext_selection_coding_system = Vselection_coding_system; setup_coding_system (Fcheck_coding_system (Vnext_selection_coding_system), &coding); + coding.src_multibyte = 1; + coding.dst_multibyte = 0; Vnext_selection_coding_system = Qnil; coding.mode |= CODING_MODE_LAST_BLOCK; Vlast_coding_system_used = coding.symbol; @@ -495,6 +537,8 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat dst = (unsigned char *) xmalloc (bufsize); encode_coding (&coding, src, dst, nbytes, bufsize); no_crlf_conversion = 1; + nbytes = coding.produced; + src = dst; } if (!open_clipboard ()) @@ -535,6 +579,9 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat case 2: message2 (binary_msg, sizeof (binary_msg) - 1, 0); break; + case 3: + message2 (system_error_msg, sizeof (system_error_msg) - 1, 0); + break; } sit_for (2, 0, 0, 1, 1); } @@ -609,16 +656,16 @@ DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_dat Vnext_selection_coding_system = Vselection_coding_system; setup_coding_system (Fcheck_coding_system (Vnext_selection_coding_system), &coding); + coding.src_multibyte = 0; + coding.dst_multibyte = 1; Vnext_selection_coding_system = Qnil; coding.mode |= CODING_MODE_LAST_BLOCK; truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 1); bufsize = decoding_buffer_size (&coding, truelen); buf = (unsigned char *) xmalloc (bufsize); decode_coding (&coding, htext, buf, truelen, bufsize); - truelen = (coding.fake_multibyte - ? multibyte_chars_in_text (buf, coding.produced) - : coding.produced_char); - ret = make_string_from_bytes ((char *) buf, truelen, coding.produced); + ret = make_string_from_bytes ((char *) buf, + coding.produced_char, coding.produced); xfree (buf); Vlast_coding_system_used = coding.symbol; }