1 /* Selection processing for Emacs on the Microsoft W32 API.
2 Copyright (C) 1993, 1994 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Written by Kevin Gallo */
25 #include "w32term.h" /* for all of the w32 includes */
26 #include "dispextern.h" /* frame.h seems to want this */
28 #include "frame.h" /* Need this to get the X window of selected_frame */
29 #include "blockinput.h"
34 Lisp_Object QCLIPBOARD
;
36 /* Coding system for communicating with other Windows programs via the
38 static Lisp_Object Vselection_coding_system
;
40 /* Coding system for the next communicating with other Windows programs. */
41 static Lisp_Object Vnext_selection_coding_system
;
43 /* The last text we put into the clipboard. This is used to prevent
44 passing back our own text from the clipboard, instead of using the
45 kill ring. The former is undesirable because the clipboard data
46 could be MULEtilated by inappropriately chosen
47 (next-)selection-coding-system. For this reason, we must store the
48 text *after* it was encoded/Unix-to-DOS-converted. */
49 static unsigned char *last_clipboard_text
= NULL
;
50 static size_t clipboard_storage_size
= 0;
53 DEFUN ("w32-open-clipboard", Fw32_open_clipboard
, Sw32_open_clipboard
, 0, 1, 0,
54 doc
: /* This opens the clipboard with the given frame pointer. */)
61 CHECK_LIVE_FRAME (frame
);
65 ok
= OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
);
69 return (ok
? frame
: Qnil
);
72 DEFUN ("w32-empty-clipboard", Fw32_empty_clipboard
,
73 Sw32_empty_clipboard
, 0, 0, 0,
74 doc
: /* Empty the clipboard.
75 Assigns ownership of the clipboard to the window which opened it. */)
82 ok
= EmptyClipboard ();
86 return (ok
? Qt
: Qnil
);
89 DEFUN ("w32-close-clipboard", Fw32_close_clipboard
,
90 Sw32_close_clipboard
, 0, 0, 0,
91 doc
: /* Close the clipboard. */)
98 ok
= CloseClipboard ();
102 return (ok
? Qt
: Qnil
);
107 DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data
,
108 Sw32_set_clipboard_data
, 1, 2, 0,
109 doc
: /* This sets the clipboard data to the given text. */)
111 Lisp_Object string
, frame
;
116 int truelen
, nlines
= 0;
120 CHECK_STRING (string
);
123 CHECK_LIVE_FRAME (frame
);
127 nbytes
= STRING_BYTES (XSTRING (string
)) + 1;
128 src
= XSTRING (string
)->data
;
131 /* We need to know how many lines there are, since we need CRLF line
132 termination for compatibility with other Windows Programs.
133 avoid using strchr because it recomputes the length every time */
134 while ((dst
= memchr (dst
, '\n', nbytes
- (dst
- src
))) != NULL
)
141 /* Since we are now handling multilingual text, we must consider
142 encoding text for the clipboard. */
143 int charset_info
= find_charset_in_text (src
, XSTRING (string
)->size
,
146 if (charset_info
== 0)
148 /* No multibyte character in OBJ. We need not encode it. */
150 /* Need to know final size after CR chars are inserted (the
151 standard CF_TEXT clipboard format uses CRLF line endings,
152 while Emacs uses just LF internally). */
154 truelen
= nbytes
+ nlines
;
156 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, truelen
)) == NULL
)
159 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
162 /* convert to CRLF line endings expected by clipboard */
166 /* copy next line or remaining bytes including '\0' */
167 next
= _memccpy (dst
, src
, '\n', nbytes
);
170 /* copied one line ending with '\n' */
171 int copied
= next
- dst
;
174 /* insert '\r' before '\n' */
180 /* copied remaining partial line -> now finished */
184 GlobalUnlock (htext
);
186 Vlast_coding_system_used
= Qraw_text
;
190 /* We must encode contents of OBJ to the selection coding
193 struct coding_system coding
;
196 if (NILP (Vnext_selection_coding_system
))
197 Vnext_selection_coding_system
= Vselection_coding_system
;
199 (Fcheck_coding_system (Vnext_selection_coding_system
), &coding
);
200 coding
.src_multibyte
= 1;
201 coding
.dst_multibyte
= 0;
202 Vnext_selection_coding_system
= Qnil
;
203 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
204 bufsize
= encoding_buffer_size (&coding
, nbytes
);
205 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, bufsize
)) == NULL
)
207 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
209 encode_coding (&coding
, src
, dst
, nbytes
, bufsize
);
210 Vlast_coding_system_used
= coding
.symbol
;
212 /* Stash away the data we are about to put into the clipboard, so we
213 could later check inside Fw32_get_clipboard_data whether
214 the clipboard still holds our data. */
215 if (clipboard_storage_size
< coding
.produced
)
217 clipboard_storage_size
= coding
.produced
+ 100;
218 last_clipboard_text
= (char *) xrealloc (last_clipboard_text
,
219 clipboard_storage_size
);
221 if (last_clipboard_text
)
222 memcpy (last_clipboard_text
, dst
, coding
.produced
);
224 GlobalUnlock (htext
);
226 /* Shrink data block to actual size. */
227 htext2
= GlobalReAlloc (htext
, coding
.produced
,
228 GMEM_MOVEABLE
| GMEM_DDESHARE
);
229 if (htext2
!= NULL
) htext
= htext2
;
233 if (!OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
))
236 ok
= EmptyClipboard () && SetClipboardData (CF_TEXT
, htext
);
245 if (htext
) GlobalFree (htext
);
246 if (last_clipboard_text
)
247 *last_clipboard_text
= '\0';
252 return (ok
? string
: Qnil
);
255 DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data
,
256 Sw32_get_clipboard_data
, 0, 1, 0,
257 doc
: /* This gets the clipboard data in text format. */)
262 Lisp_Object ret
= Qnil
;
265 CHECK_LIVE_FRAME (frame
);
269 if (!OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
))
272 if ((htext
= GetClipboardData (CF_TEXT
)) == NULL
)
280 int require_decoding
= 0;
282 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
285 nbytes
= strlen (src
);
287 /* If the text in clipboard is identical to what we put there
288 last time w32_set_clipboard_data was called, pretend there's no
289 data in the clipboard. This is so we don't pass our own text
290 from the clipboard (which might be troublesome if the killed
291 text includes null characters). */
292 if (last_clipboard_text
293 && clipboard_storage_size
>= nbytes
294 && memcmp(last_clipboard_text
, src
, nbytes
) == 0)
301 ! NILP (buffer_defaults
.enable_multibyte_characters
)
305 /* If the clipboard data contains any non-ascii code, we
306 need to decode it. */
309 for (i
= 0; i
< nbytes
; i
++)
313 require_decoding
= 1;
319 if (require_decoding
)
323 struct coding_system coding
;
325 if (NILP (Vnext_selection_coding_system
))
326 Vnext_selection_coding_system
= Vselection_coding_system
;
328 (Fcheck_coding_system (Vnext_selection_coding_system
), &coding
);
329 coding
.src_multibyte
= 0;
330 coding
.dst_multibyte
= 1;
331 Vnext_selection_coding_system
= Qnil
;
332 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
333 bufsize
= decoding_buffer_size (&coding
, nbytes
);
334 buf
= (unsigned char *) xmalloc (bufsize
);
335 decode_coding (&coding
, src
, buf
, nbytes
, bufsize
);
336 Vlast_coding_system_used
= coding
.symbol
;
337 ret
= make_string_from_bytes ((char *) buf
,
338 coding
.produced_char
, coding
.produced
);
343 /* Need to know final size after CR chars are removed because we
344 can't change the string size manually, and doing an extra
345 copy is silly. Note that we only remove CR when it appears
350 /* avoid using strchr because it recomputes the length everytime */
351 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
353 if (dst
[1] == '\n') /* safe because of trailing '\0' */
358 ret
= make_uninit_string (truelen
);
360 /* Convert CRLF line endings (the standard CF_TEXT clipboard
361 format) to LF endings as used internally by Emacs. */
363 dst
= XSTRING (ret
)->data
;
367 /* copy next line or remaining bytes excluding '\0' */
368 next
= _memccpy (dst
, src
, '\r', nbytes
);
371 /* copied one line ending with '\r' */
372 int copied
= next
- dst
;
377 dst
--; /* overwrite '\r' with '\n' */
380 /* copied remaining partial line -> now finished */
384 Vlast_coding_system_used
= Qraw_text
;
387 GlobalUnlock (htext
);
399 /* Support checking for a clipboard selection. */
401 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
403 doc
: /* Whether there is an owner for the given X Selection.
404 The arg should be the name of the selection in question, typically one of
405 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
406 \(Those are literal upper-case symbol names, since that's what X expects.)
407 For convenience, the symbol nil is the same as `PRIMARY',
408 and t is the same as `SECONDARY'. */)
410 Lisp_Object selection
;
412 CHECK_SYMBOL (selection
);
414 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
415 if the clipboard currently has valid text format contents. */
417 if (EQ (selection
, QCLIPBOARD
))
419 Lisp_Object val
= Qnil
;
421 if (OpenClipboard (NULL
))
424 while (format
= EnumClipboardFormats (format
))
425 if (format
== CF_TEXT
)
441 defsubr (&Sw32_open_clipboard
);
442 defsubr (&Sw32_empty_clipboard
);
443 defsubr (&Sw32_close_clipboard
);
445 defsubr (&Sw32_set_clipboard_data
);
446 defsubr (&Sw32_get_clipboard_data
);
447 defsubr (&Sx_selection_exists_p
);
449 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system
,
450 doc
: /* Coding system for communicating with other programs.
451 When sending or receiving text via cut_buffer, selection, and clipboard,
452 the text is encoded or decoded by this coding system. */);
453 Vselection_coding_system
=intern ("iso-latin-1-dos");
455 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system
,
456 doc
: /* Coding system for the next communication with other programs.
457 Usually, `selection-coding-system' is used for communicating with
458 other programs. But, if this variable is set, it is used for the
459 next communication only. After the communication, this variable is
461 Vnext_selection_coding_system
= Qnil
;
463 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);