1 /* Win32 Selection processing for emacs
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 win32 includes */
26 #include "dispextern.h" /* frame.h seems to want this */
27 #include "frame.h" /* Need this to get the X window of selected_frame */
28 #include "blockinput.h"
30 Lisp_Object QCLIPBOARD
;
33 DEFUN ("win32-open-clipboard", Fwin32_open_clipboard
, Swin32_open_clipboard
, 0, 1, 0,
34 "This opens the clipboard with the given frame pointer.")
41 CHECK_LIVE_FRAME (frame
, 0);
45 ok
= OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
);
49 return (ok
? frame
: Qnil
);
52 DEFUN ("win32-empty-clipboard", Fwin32_empty_clipboard
, Swin32_empty_clipboard
, 0, 0, 0,
53 "This empties the clipboard and assigns ownership to the window which opened the clipboard.")
60 ok
= EmptyClipboard ();
64 return (ok
? Qt
: Qnil
);
67 DEFUN ("win32-close-clipboard", Fwin32_close_clipboard
, Swin32_close_clipboard
, 0, 0, 0,
68 "This closes the clipboard.")
75 ok
= CloseClipboard ();
79 return (ok
? Qt
: Qnil
);
84 DEFUN ("win32-set-clipboard-data", Fwin32_set_clipboard_data
, Swin32_set_clipboard_data
, 1, 2, 0,
85 "This sets the clipboard data to the given text.")
87 Lisp_Object string
, frame
;
96 CHECK_STRING (string
, 0);
99 CHECK_LIVE_FRAME (frame
, 0);
103 nbytes
= XSTRING (string
)->size
+ 1;
104 src
= XSTRING (string
)->data
;
106 /* need to know final size after '\r' chars are inserted (the
107 standard CF_TEXT clipboard format uses CRLF line endings,
108 while Emacs uses just LF internally) */
112 /* avoid using strchr because it recomputes the length everytime */
113 while ((dst
= memchr (dst
, '\n', nbytes
- (dst
- src
))) != NULL
)
119 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, truelen
)) == NULL
)
122 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
125 /* convert to CRLF line endings expected by clipboard */
129 /* copy next line or remaining bytes including '\0' */
130 next
= _memccpy (dst
, src
, '\n', nbytes
);
133 /* copied one line ending with '\n' */
134 int copied
= next
- dst
;
137 /* insert '\r' before '\n' */
143 /* copied remaining partial line -> now finished */
147 GlobalUnlock (htext
);
149 if (!OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
))
152 ok
= EmptyClipboard () && SetClipboardData (CF_TEXT
, htext
);
161 if (htext
) GlobalFree (htext
);
166 return (ok
? string
: Qnil
);
169 DEFUN ("win32-get-clipboard-data", Fwin32_get_clipboard_data
, Swin32_get_clipboard_data
, 0, 1, 0,
170 "This gets the clipboard data in text format.")
175 Lisp_Object ret
= Qnil
;
178 CHECK_LIVE_FRAME (frame
, 0);
182 if (!OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
))
185 if ((htext
= GetClipboardData (CF_TEXT
)) == NULL
)
194 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
197 nbytes
= strlen (src
);
199 /* need to know final size after '\r' chars are removed because
200 we can't change the string size manually, and doing an extra
205 /* avoid using strchr because it recomputes the length everytime */
206 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
212 ret
= make_uninit_string (truelen
);
214 /* convert CRLF line endings (the standard CF_TEXT clipboard
215 format) to LF endings as used internally by Emacs */
217 dst
= XSTRING (ret
)->data
;
221 /* copy next line or remaining bytes excluding '\0' */
222 next
= _memccpy (dst
, src
, '\r', nbytes
);
225 /* copied one line ending with '\r' */
226 int copied
= next
- dst
;
228 dst
+= copied
- 1; /* overwrite '\r' */
232 /* copied remaining partial line -> now finished */
236 GlobalUnlock (htext
);
248 /* Support checking for a clipboard selection. */
250 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
252 "Whether there is an owner for the given X Selection.\n\
253 The arg should be the name of the selection in question, typically one of\n\
254 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.\n\
255 \(Those are literal upper-case symbol names, since that's what X expects.)\n\
256 For convenience, the symbol nil is the same as `PRIMARY',\n\
257 and t is the same as `SECONDARY'.")
259 Lisp_Object selection
;
261 CHECK_SYMBOL (selection
, 0);
263 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
264 if the clipboard currently has valid text format contents. */
266 if (EQ (selection
, QCLIPBOARD
))
268 Lisp_Object val
= Qnil
;
270 if (OpenClipboard (NULL
))
273 while (format
= EnumClipboardFormats (format
))
274 if (format
== CF_TEXT
)
287 syms_of_win32select ()
290 defsubr (&Swin32_open_clipboard
);
291 defsubr (&Swin32_empty_clipboard
);
292 defsubr (&Swin32_close_clipboard
);
294 defsubr (&Swin32_set_clipboard_data
);
295 defsubr (&Swin32_get_clipboard_data
);
296 defsubr (&Sx_selection_exists_p
);
298 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);