(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / src / w32select.c
blobb99e94a64729a2567555caadd45318b80a276462
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)
9 any later version.
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 */
23 #include <config.h>
24 #include "lisp.h"
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 #if 0
31 DEFUN ("win32-open-clipboard", Fwin32_open_clipboard, Swin32_open_clipboard, 0, 1, 0,
32 "This opens the clipboard with the given frame pointer.")
33 (frame)
34 Lisp_Object frame;
36 BOOL ok = FALSE;
38 if (!NILP (frame))
39 CHECK_LIVE_FRAME (frame, 0);
41 BLOCK_INPUT;
43 ok = OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL);
45 UNBLOCK_INPUT;
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.")
54 BOOL ok = FALSE;
56 BLOCK_INPUT;
58 ok = EmptyClipboard ();
60 UNBLOCK_INPUT;
62 return (ok ? Qt : Qnil);
65 DEFUN ("win32-close-clipboard", Fwin32_close_clipboard, Swin32_close_clipboard, 0, 0, 0,
66 "This closes the clipboard.")
69 BOOL ok = FALSE;
71 BLOCK_INPUT;
73 ok = CloseClipboard ();
75 UNBLOCK_INPUT;
77 return (ok ? Qt : Qnil);
80 #endif
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.")
84 (string, frame)
85 Lisp_Object string, frame;
87 BOOL ok = TRUE;
88 HANDLE htext;
89 int nbytes;
90 int truelen;
91 unsigned char *src;
92 unsigned char *dst;
94 CHECK_STRING (string, 0);
96 if (!NILP (frame))
97 CHECK_LIVE_FRAME (frame, 0);
99 BLOCK_INPUT;
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) */
108 truelen = nbytes;
109 dst = src;
110 /* avoid using strchr because it recomputes the length everytime */
111 while ((dst = memchr (dst, '\n', nbytes - (dst - src))) != NULL)
113 truelen++;
114 dst++;
117 if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, truelen)) == NULL)
118 goto error;
120 if ((dst = (unsigned char *) GlobalLock (htext)) == NULL)
121 goto error;
123 /* convert to CRLF line endings expected by clipboard */
124 while (1)
126 unsigned char *next;
127 /* copy next line or remaining bytes including '\0' */
128 next = _memccpy (dst, src, '\n', nbytes);
129 if (next)
131 /* copied one line ending with '\n' */
132 int copied = next - dst;
133 nbytes -= copied;
134 src += copied;
135 /* insert '\r' before '\n' */
136 next[-1] = '\r';
137 next[0] = '\n';
138 dst = next + 1;
140 else
141 /* copied remaining partial line -> now finished */
142 break;
145 GlobalUnlock (htext);
147 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
148 goto error;
150 ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext);
152 CloseClipboard ();
154 if (ok) goto done;
156 error:
158 ok = FALSE;
159 if (htext) GlobalFree (htext);
161 done:
162 UNBLOCK_INPUT;
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.")
169 (frame)
170 Lisp_Object frame;
172 HANDLE htext;
173 Lisp_Object ret = Qnil;
175 if (!NILP (frame))
176 CHECK_LIVE_FRAME (frame, 0);
178 BLOCK_INPUT;
180 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
181 goto done;
183 if ((htext = GetClipboardData (CF_TEXT)) == NULL)
184 goto closeclip;
187 unsigned char *src;
188 unsigned char *dst;
189 int nbytes;
190 int truelen;
192 if ((src = (unsigned char *) GlobalLock (htext)) == NULL)
193 goto closeclip;
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
199 copy is silly */
201 truelen = nbytes;
202 dst = src;
203 /* avoid using strchr because it recomputes the length everytime */
204 while ((dst = memchr (dst, '\r', nbytes - (dst - src))) != NULL)
206 truelen--;
207 dst++;
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;
216 while (1)
218 unsigned char *next;
219 /* copy next line or remaining bytes excluding '\0' */
220 next = _memccpy (dst, src, '\r', nbytes);
221 if (next)
223 /* copied one line ending with '\r' */
224 int copied = next - dst;
225 nbytes -= copied;
226 dst += copied - 1; /* overwrite '\r' */
227 src += copied;
229 else
230 /* copied remaining partial line -> now finished */
231 break;
234 GlobalUnlock (htext);
237 closeclip:
238 CloseClipboard ();
240 done:
241 UNBLOCK_INPUT;
243 return (ret);
246 void
247 syms_of_win32select ()
249 #if 0
250 defsubr (&Swin32_open_clipboard);
251 defsubr (&Swin32_empty_clipboard);
252 defsubr (&Swin32_close_clipboard);
253 #endif
254 defsubr (&Swin32_set_clipboard_data);
255 defsubr (&Swin32_get_clipboard_data);