(imenu--in-alist): Accept only bottom-level matches.
[emacs.git] / src / w32select.c
blob6b3b0acf8b0beaf137a30353334b20dc062e5f91
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 Lisp_Object QCLIPBOARD;
32 #if 0
33 DEFUN ("win32-open-clipboard", Fwin32_open_clipboard, Swin32_open_clipboard, 0, 1, 0,
34 "This opens the clipboard with the given frame pointer.")
35 (frame)
36 Lisp_Object frame;
38 BOOL ok = FALSE;
40 if (!NILP (frame))
41 CHECK_LIVE_FRAME (frame, 0);
43 BLOCK_INPUT;
45 ok = OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL);
47 UNBLOCK_INPUT;
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.")
56 BOOL ok = FALSE;
58 BLOCK_INPUT;
60 ok = EmptyClipboard ();
62 UNBLOCK_INPUT;
64 return (ok ? Qt : Qnil);
67 DEFUN ("win32-close-clipboard", Fwin32_close_clipboard, Swin32_close_clipboard, 0, 0, 0,
68 "This closes the clipboard.")
71 BOOL ok = FALSE;
73 BLOCK_INPUT;
75 ok = CloseClipboard ();
77 UNBLOCK_INPUT;
79 return (ok ? Qt : Qnil);
82 #endif
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.")
86 (string, frame)
87 Lisp_Object string, frame;
89 BOOL ok = TRUE;
90 HANDLE htext;
91 int nbytes;
92 int truelen;
93 unsigned char *src;
94 unsigned char *dst;
96 CHECK_STRING (string, 0);
98 if (!NILP (frame))
99 CHECK_LIVE_FRAME (frame, 0);
101 BLOCK_INPUT;
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) */
110 truelen = nbytes;
111 dst = src;
112 /* avoid using strchr because it recomputes the length everytime */
113 while ((dst = memchr (dst, '\n', nbytes - (dst - src))) != NULL)
115 truelen++;
116 dst++;
119 if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, truelen)) == NULL)
120 goto error;
122 if ((dst = (unsigned char *) GlobalLock (htext)) == NULL)
123 goto error;
125 /* convert to CRLF line endings expected by clipboard */
126 while (1)
128 unsigned char *next;
129 /* copy next line or remaining bytes including '\0' */
130 next = _memccpy (dst, src, '\n', nbytes);
131 if (next)
133 /* copied one line ending with '\n' */
134 int copied = next - dst;
135 nbytes -= copied;
136 src += copied;
137 /* insert '\r' before '\n' */
138 next[-1] = '\r';
139 next[0] = '\n';
140 dst = next + 1;
142 else
143 /* copied remaining partial line -> now finished */
144 break;
147 GlobalUnlock (htext);
149 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
150 goto error;
152 ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext);
154 CloseClipboard ();
156 if (ok) goto done;
158 error:
160 ok = FALSE;
161 if (htext) GlobalFree (htext);
163 done:
164 UNBLOCK_INPUT;
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.")
171 (frame)
172 Lisp_Object frame;
174 HANDLE htext;
175 Lisp_Object ret = Qnil;
177 if (!NILP (frame))
178 CHECK_LIVE_FRAME (frame, 0);
180 BLOCK_INPUT;
182 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
183 goto done;
185 if ((htext = GetClipboardData (CF_TEXT)) == NULL)
186 goto closeclip;
189 unsigned char *src;
190 unsigned char *dst;
191 int nbytes;
192 int truelen;
194 if ((src = (unsigned char *) GlobalLock (htext)) == NULL)
195 goto closeclip;
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
201 copy is silly */
203 truelen = nbytes;
204 dst = src;
205 /* avoid using strchr because it recomputes the length everytime */
206 while ((dst = memchr (dst, '\r', nbytes - (dst - src))) != NULL)
208 truelen--;
209 dst++;
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;
218 while (1)
220 unsigned char *next;
221 /* copy next line or remaining bytes excluding '\0' */
222 next = _memccpy (dst, src, '\r', nbytes);
223 if (next)
225 /* copied one line ending with '\r' */
226 int copied = next - dst;
227 nbytes -= copied;
228 dst += copied - 1; /* overwrite '\r' */
229 src += copied;
231 else
232 /* copied remaining partial line -> now finished */
233 break;
236 GlobalUnlock (htext);
239 closeclip:
240 CloseClipboard ();
242 done:
243 UNBLOCK_INPUT;
245 return (ret);
248 /* Support checking for a clipboard selection. */
250 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
251 0, 1, 0,
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'.")
258 (selection)
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))
272 int format = 0;
273 while (format = EnumClipboardFormats (format))
274 if (format == CF_TEXT)
276 val = Qt;
277 break;
279 CloseClipboard ();
281 return val;
283 return Qnil;
286 void
287 syms_of_win32select ()
289 #if 0
290 defsubr (&Swin32_open_clipboard);
291 defsubr (&Swin32_empty_clipboard);
292 defsubr (&Swin32_close_clipboard);
293 #endif
294 defsubr (&Swin32_set_clipboard_data);
295 defsubr (&Swin32_get_clipboard_data);
296 defsubr (&Sx_selection_exists_p);
298 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);