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"
33 #include "composite.h"
35 Lisp_Object QCLIPBOARD
;
37 /* Coding system for communicating with other Windows programs via the
39 static Lisp_Object Vselection_coding_system
;
41 /* Coding system for the next communicating with other Windows programs. */
42 static Lisp_Object Vnext_selection_coding_system
;
44 /* Sequence number, used where possible to detect when we are pasting
46 static DWORD last_clipboard_sequence_number
;
47 extern ClipboardSequence_Proc clipboard_sequence_fn
;
49 /* The last text we put into the clipboard. This is used when the OS
50 does not support sequence numbers (NT4, 95). It is undesirable to
51 use data put on the clipboard by Emacs because the clipboard data
52 could be MULEtilated by inappropriately chosen
53 (next-)selection-coding-system. For this reason, we must store the
54 text *after* it was encoded/Unix-to-DOS-converted. */
55 static unsigned char *last_clipboard_text
= NULL
;
56 static size_t clipboard_storage_size
= 0;
59 DEFUN ("w32-open-clipboard", Fw32_open_clipboard
, Sw32_open_clipboard
, 0, 1, 0,
60 doc
: /* This opens the clipboard with the given frame pointer. */)
67 CHECK_LIVE_FRAME (frame
);
71 ok
= OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
);
75 return (ok
? frame
: Qnil
);
78 DEFUN ("w32-empty-clipboard", Fw32_empty_clipboard
,
79 Sw32_empty_clipboard
, 0, 0, 0,
80 doc
: /* Empty the clipboard.
81 Assigns ownership of the clipboard to the window which opened it. */)
88 ok
= EmptyClipboard ();
92 return (ok
? Qt
: Qnil
);
95 DEFUN ("w32-close-clipboard", Fw32_close_clipboard
,
96 Sw32_close_clipboard
, 0, 0, 0,
97 doc
: /* Close the clipboard. */)
104 ok
= CloseClipboard ();
108 return (ok
? Qt
: Qnil
);
113 DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data
,
114 Sw32_set_clipboard_data
, 1, 2, 0,
115 doc
: /* This sets the clipboard data to the given text. */)
117 Lisp_Object string
, frame
;
122 int truelen
, nlines
= 0;
126 CHECK_STRING (string
);
129 CHECK_LIVE_FRAME (frame
);
133 nbytes
= SBYTES (string
) + 1;
134 src
= SDATA (string
);
137 /* We need to know how many lines there are, since we need CRLF line
138 termination for compatibility with other Windows Programs.
139 avoid using strchr because it recomputes the length every time */
140 while ((dst
= memchr (dst
, '\n', nbytes
- (dst
- src
))) != NULL
)
147 /* Since we are now handling multilingual text, we must consider
148 encoding text for the clipboard. */
149 int charset_info
= find_charset_in_text (src
, SCHARS (string
),
152 if (charset_info
== 0)
154 /* No multibyte character in OBJ. We need not encode it. */
156 /* Need to know final size after CR chars are inserted (the
157 standard CF_TEXT clipboard format uses CRLF line endings,
158 while Emacs uses just LF internally). */
160 truelen
= nbytes
+ nlines
;
162 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, truelen
)) == NULL
)
165 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
168 /* convert to CRLF line endings expected by clipboard */
172 /* copy next line or remaining bytes including '\0' */
173 next
= _memccpy (dst
, src
, '\n', nbytes
);
176 /* copied one line ending with '\n' */
177 int copied
= next
- dst
;
180 /* insert '\r' before '\n' */
186 /* copied remaining partial line -> now finished */
190 GlobalUnlock (htext
);
192 Vlast_coding_system_used
= Qraw_text
;
196 /* We must encode contents of OBJ to the selection coding
199 struct coding_system coding
;
202 if (NILP (Vnext_selection_coding_system
))
203 Vnext_selection_coding_system
= Vselection_coding_system
;
205 (Fcheck_coding_system (Vnext_selection_coding_system
), &coding
);
206 if (SYMBOLP (coding
.pre_write_conversion
)
207 && !NILP (Ffboundp (coding
.pre_write_conversion
)))
209 string
= run_pre_post_conversion_on_str (string
, &coding
, 1);
210 src
= SDATA (string
);
211 nbytes
= SBYTES (string
);
213 coding
.src_multibyte
= 1;
214 coding
.dst_multibyte
= 0;
215 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
216 encode_coding_iso2022 trying to dereference a null pointer. */
217 coding
.composing
= COMPOSITION_DISABLED
;
218 if (coding
.type
== coding_type_iso2022
)
219 coding
.flags
|= CODING_FLAG_ISO_SAFE
;
220 Vnext_selection_coding_system
= Qnil
;
221 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
222 bufsize
= encoding_buffer_size (&coding
, nbytes
);
223 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, bufsize
)) == NULL
)
225 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
227 encode_coding (&coding
, src
, dst
, nbytes
, bufsize
);
228 Vlast_coding_system_used
= coding
.symbol
;
230 /* If clipboard sequence numbers are not supported, keep a copy for
232 if (!clipboard_sequence_fn
)
234 /* Stash away the data we are about to put into the
235 clipboard, so we could later check inside
236 Fw32_get_clipboard_data whether the clipboard still
238 if (clipboard_storage_size
< coding
.produced
)
240 clipboard_storage_size
= coding
.produced
+ 100;
241 last_clipboard_text
= (char *) xrealloc (last_clipboard_text
,
242 clipboard_storage_size
);
244 if (last_clipboard_text
)
245 memcpy (last_clipboard_text
, dst
, coding
.produced
);
248 GlobalUnlock (htext
);
250 /* Shrink data block to actual size. */
251 htext2
= GlobalReAlloc (htext
, coding
.produced
,
252 GMEM_MOVEABLE
| GMEM_DDESHARE
);
253 if (htext2
!= NULL
) htext
= htext2
;
257 if (!OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
))
260 ok
= EmptyClipboard () && SetClipboardData (CF_TEXT
, htext
);
264 /* Common sense says to read the sequence number inside the
265 OpenClipboard/ CloseClipboard block to avoid race conditions
266 where another app puts something on the clipboard straight after
267 us. But experience suggests that the sequence number from the
268 SetClipboardData is not allocated until we close the clipboard!
269 Since clipboard operations are normally user-driven, the race
270 condition is probably not going to really happen. */
271 if (clipboard_sequence_fn
)
272 last_clipboard_sequence_number
= clipboard_sequence_fn ();
279 if (htext
) GlobalFree (htext
);
280 if (last_clipboard_text
)
281 *last_clipboard_text
= '\0';
283 last_clipboard_sequence_number
= 0;
288 return (ok
? string
: Qnil
);
291 DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data
,
292 Sw32_get_clipboard_data
, 0, 1, 0,
293 doc
: /* This gets the clipboard data in text format. */)
298 Lisp_Object ret
= Qnil
;
301 CHECK_LIVE_FRAME (frame
);
305 if (!OpenClipboard ((!NILP (frame
) && FRAME_W32_P (XFRAME (frame
))) ? FRAME_W32_WINDOW (XFRAME (frame
)) : NULL
))
308 if ((htext
= GetClipboardData (CF_TEXT
)) == NULL
)
316 int require_decoding
= 0;
318 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
321 nbytes
= strlen (src
);
323 /* If the text in clipboard is identical to what we put there
324 last time w32_set_clipboard_data was called, pretend there's no
325 data in the clipboard. This is so we don't pass our own text
326 from the clipboard (which might be troublesome if the killed
327 text includes null characters). */
328 if ((clipboard_sequence_fn
329 && clipboard_sequence_fn () == last_clipboard_sequence_number
)
330 || (last_clipboard_text
331 && clipboard_storage_size
>= nbytes
332 && memcmp(last_clipboard_text
, src
, nbytes
) == 0))
336 /* If the clipboard data contains any non-ascii code, we
337 need to decode it. */
340 for (i
= 0; i
< nbytes
; i
++)
344 require_decoding
= 1;
350 if (require_decoding
)
354 struct coding_system coding
;
356 if (NILP (Vnext_selection_coding_system
))
357 Vnext_selection_coding_system
= Vselection_coding_system
;
359 (Fcheck_coding_system (Vnext_selection_coding_system
), &coding
);
360 coding
.src_multibyte
= 0;
361 coding
.dst_multibyte
= 1;
362 Vnext_selection_coding_system
= Qnil
;
363 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
364 /* We explicitely disable composition handling because
365 selection data should not contain any composition
367 coding
.composing
= COMPOSITION_DISABLED
;
368 bufsize
= decoding_buffer_size (&coding
, nbytes
);
369 buf
= (unsigned char *) xmalloc (bufsize
);
370 decode_coding (&coding
, src
, buf
, nbytes
, bufsize
);
371 Vlast_coding_system_used
= coding
.symbol
;
372 ret
= make_string_from_bytes ((char *) buf
,
373 coding
.produced_char
, coding
.produced
);
375 if (SYMBOLP (coding
.post_read_conversion
)
376 && !NILP (Ffboundp (coding
.post_read_conversion
)))
377 ret
= run_pre_post_conversion_on_str (ret
, &coding
, 0);
381 /* Need to know final size after CR chars are removed because we
382 can't change the string size manually, and doing an extra
383 copy is silly. Note that we only remove CR when it appears
388 /* avoid using strchr because it recomputes the length everytime */
389 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
391 if (dst
[1] == '\n') /* safe because of trailing '\0' */
396 ret
= make_uninit_string (truelen
);
398 /* Convert CRLF line endings (the standard CF_TEXT clipboard
399 format) to LF endings as used internally by Emacs. */
405 /* copy next line or remaining bytes excluding '\0' */
406 next
= _memccpy (dst
, src
, '\r', nbytes
);
409 /* copied one line ending with '\r' */
410 int copied
= next
- dst
;
415 dst
--; /* overwrite '\r' with '\n' */
418 /* copied remaining partial line -> now finished */
422 Vlast_coding_system_used
= Qraw_text
;
425 GlobalUnlock (htext
);
437 /* Support checking for a clipboard selection. */
439 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
441 doc
: /* Whether there is an owner for the given X Selection.
442 The arg should be the name of the selection in question, typically one of
443 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
444 \(Those are literal upper-case symbol names, since that's what X expects.)
445 For convenience, the symbol nil is the same as `PRIMARY',
446 and t is the same as `SECONDARY'. */)
448 Lisp_Object selection
;
450 CHECK_SYMBOL (selection
);
452 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
453 if the clipboard currently has valid text format contents. */
455 if (EQ (selection
, QCLIPBOARD
))
457 Lisp_Object val
= Qnil
;
459 if (OpenClipboard (NULL
))
462 while (format
= EnumClipboardFormats (format
))
463 if (format
== CF_TEXT
)
479 defsubr (&Sw32_open_clipboard
);
480 defsubr (&Sw32_empty_clipboard
);
481 defsubr (&Sw32_close_clipboard
);
483 defsubr (&Sw32_set_clipboard_data
);
484 defsubr (&Sw32_get_clipboard_data
);
485 defsubr (&Sx_selection_exists_p
);
487 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system
,
488 doc
: /* Coding system for communicating with other programs.
489 When sending or receiving text via cut_buffer, selection, and clipboard,
490 the text is encoded or decoded by this coding system.
491 The default value is `iso-latin-1-dos'. */);
492 Vselection_coding_system
= intern ("iso-latin-1-dos");
494 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system
,
495 doc
: /* Coding system for the next communication with other programs.
496 Usually, `selection-coding-system' is used for communicating with
497 other programs. But, if this variable is set, it is used for the
498 next communication only. After the communication, this variable is
500 Vnext_selection_coding_system
= Qnil
;
502 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);
505 /* arch-tag: c96e9724-5eb1-4dad-be07-289f092fd2af
506 (do not change this comment) */