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"
31 DEFUN ("win32-open-clipboard", Fwin32_open_clipboard
, Swin32_open_clipboard
, 0, 1, 0,
32 "This opens the clipboard with the given frame pointer.")
39 CHECK_LIVE_FRAME (frame
, 0);
43 ok
= OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
);
47 return (ok
? frame
: Qnil
);
50 DEFUN ("win32-empty-clipboard", Fwin32_empty_clipboard
, Swin32_empty_clipboard
, 0, 0, 0,
51 "This empties the clipboard and assigns ownership to the window which opened the clipboard.")
58 ok
= EmptyClipboard ();
62 return (ok
? Qt
: Qnil
);
65 DEFUN ("win32-close-clipboard", Fwin32_close_clipboard
, Swin32_close_clipboard
, 0, 0, 0,
66 "This closes the clipboard.")
73 ok
= CloseClipboard ();
77 return (ok
? Qt
: Qnil
);
82 DEFUN ("win32-set-clipboard-data", Fwin32_set_clipboard_data
, Swin32_set_clipboard_data
, 1, 2, 0,
83 "This sets the clipboard data to the given text.")
85 Lisp_Object string
, frame
;
94 CHECK_STRING (string
, 0);
97 CHECK_LIVE_FRAME (frame
, 0);
101 nbytes
= XSTRING (string
)->size
+ 1;
102 src
= XSTRING (string
)->data
;
104 /* need to know final size after '\r' chars are inserted (the
105 standard CF_TEXT clipboard format uses CRLF line endings,
106 while Emacs uses just LF internally) */
110 /* avoid using strchr because it recomputes the length everytime */
111 while ((dst
= memchr (dst
, '\n', nbytes
- (dst
- src
))) != NULL
)
117 if ((htext
= GlobalAlloc (GMEM_MOVEABLE
| GMEM_DDESHARE
, truelen
)) == NULL
)
120 if ((dst
= (unsigned char *) GlobalLock (htext
)) == NULL
)
123 /* convert to CRLF line endings expected by clipboard */
127 /* copy next line or remaining bytes including '\0' */
128 next
= _memccpy (dst
, src
, '\n', nbytes
);
131 /* copied one line ending with '\n' */
132 int copied
= next
- dst
;
135 /* insert '\r' before '\n' */
141 /* copied remaining partial line -> now finished */
145 GlobalUnlock (htext
);
147 if (!OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
))
150 ok
= EmptyClipboard () && SetClipboardData (CF_TEXT
, htext
);
159 if (htext
) GlobalFree (htext
);
164 return (ok
? string
: Qnil
);
167 DEFUN ("win32-get-clipboard-data", Fwin32_get_clipboard_data
, Swin32_get_clipboard_data
, 0, 1, 0,
168 "This gets the clipboard data in text format.")
173 Lisp_Object ret
= Qnil
;
176 CHECK_LIVE_FRAME (frame
, 0);
180 if (!OpenClipboard ((!NILP (frame
) && FRAME_WIN32_P (XFRAME (frame
))) ? FRAME_WIN32_WINDOW (XFRAME (frame
)) : NULL
))
183 if ((htext
= GetClipboardData (CF_TEXT
)) == NULL
)
192 if ((src
= (unsigned char *) GlobalLock (htext
)) == NULL
)
195 nbytes
= strlen (src
);
197 /* need to know final size after '\r' chars are removed because
198 we can't change the string size manually, and doing an extra
203 /* avoid using strchr because it recomputes the length everytime */
204 while ((dst
= memchr (dst
, '\r', nbytes
- (dst
- src
))) != NULL
)
210 ret
= make_uninit_string (truelen
);
212 /* convert CRLF line endings (the standard CF_TEXT clipboard
213 format) to LF endings as used internally by Emacs */
215 dst
= XSTRING (ret
)->data
;
219 /* copy next line or remaining bytes excluding '\0' */
220 next
= _memccpy (dst
, src
, '\r', nbytes
);
223 /* copied one line ending with '\r' */
224 int copied
= next
- dst
;
226 dst
+= copied
- 1; /* overwrite '\r' */
230 /* copied remaining partial line -> now finished */
234 GlobalUnlock (htext
);
247 syms_of_win32select ()
250 defsubr (&Swin32_open_clipboard
);
251 defsubr (&Swin32_empty_clipboard
);
252 defsubr (&Swin32_close_clipboard
);
254 defsubr (&Swin32_set_clipboard_data
);
255 defsubr (&Swin32_get_clipboard_data
);