/* 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;
\f
/* Emulation of `__dpmi_int' and friends for DJGPP v1.x */
/* Move the buffer into the low memory, convert LF into CR-LF if needed. */
if (Raw)
- dosmemput (Data, truelen, __tb);
+ {
+ dosmemput (Data, Size, xbuf_addr);
+
+ /* Terminate with a null, otherwise Windows does strange things
+ when the text size is an integral multiple of 32 bytes. */
+ _farpokeb (_dos_ds, xbuf_addr + Size, '\0');
+ }
else
{
dp = Data;
_farnspokeb (buf_offset++, '\r');
_farnspokeb (buf_offset++, *dp++);
}
+
+ /* Terminate with a null, otherwise Windows does strange things
+ when the text size is an integral multiple of 32 bytes. */
+ _farnspokeb (buf_offset, '\0');
}
- /* Terminate with a null, otherwise Windows does strange things when
- the text size is an integral multiple of 32 bytes. */
- _farnspokeb (buf_offset, *dp);
+ /* 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
free_xfer_buf ();
+ /* 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 or 2) it's an error. */
return regs.x.ax > 0 ? 0 : 1;
}
__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;
__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);
{
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 ();
dst = (unsigned char *) xmalloc (bufsize);
encode_coding (&coding, src, dst, nbytes, bufsize);
no_crlf_conversion = 1;
+ nbytes = coding.produced;
+ src = dst;
}
if (!open_clipboard ())