Upgraded to mh-e version 6.1.1.
[bpt/emacs.git] / src / xselect.c
CommitLineData
2408b3a1 1/* X Selection processing for Emacs.
d9c0d4a3
GM
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001
3 Free Software Foundation.
ede4db72
RS
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
ede4db72 21
c6c5df7f 22
ede4db72
RS
23/* Rewritten by jwz */
24
18160b98 25#include <config.h>
ede4db72
RS
26#include "lisp.h"
27#include "xterm.h" /* for all of the X includes */
7da64e5c
RS
28#include "dispextern.h" /* frame.h seems to want this */
29#include "frame.h" /* Need this to get the X window of selected_frame */
9ac0d9e0 30#include "blockinput.h"
5faa9b45 31#include "buffer.h"
e6c7c988
KH
32#include "charset.h"
33#include "coding.h"
dfcf069d 34#include "process.h"
0caef0ec 35#include "composite.h"
7da64e5c 36
d9c0d4a3
GM
37struct prop_location;
38
39static Lisp_Object x_atom_to_symbol P_ ((Display *dpy, Atom atom));
40static Atom symbol_to_x_atom P_ ((struct x_display_info *, Display *,
41 Lisp_Object));
42static void x_own_selection P_ ((Lisp_Object, Lisp_Object));
5109c8dd 43static Lisp_Object x_get_local_selection P_ ((Lisp_Object, Lisp_Object, int));
d9c0d4a3
GM
44static void x_decline_selection_request P_ ((struct input_event *));
45static Lisp_Object x_selection_request_lisp_error P_ ((Lisp_Object));
46static Lisp_Object queue_selection_requests_unwind P_ ((Lisp_Object));
47static Lisp_Object some_frame_on_display P_ ((struct x_display_info *));
48static void x_reply_selection_request P_ ((struct input_event *, int,
49 unsigned char *, int, Atom));
50static int waiting_for_other_props_on_window P_ ((Display *, Window));
51static struct prop_location *expect_property_change P_ ((Display *, Window,
52 Atom, int));
53static void unexpect_property_change P_ ((struct prop_location *));
54static Lisp_Object wait_for_property_change_unwind P_ ((Lisp_Object));
55static void wait_for_property_change P_ ((struct prop_location *));
56static Lisp_Object x_get_foreign_selection P_ ((Lisp_Object, Lisp_Object));
57static void x_get_window_property P_ ((Display *, Window, Atom,
58 unsigned char **, int *,
59 Atom *, int *, unsigned long *, int));
60static void receive_incremental_selection P_ ((Display *, Window, Atom,
61 Lisp_Object, unsigned,
62 unsigned char **, int *,
63 Atom *, int *, unsigned long *));
64static Lisp_Object x_get_window_property_as_lisp_data P_ ((Display *,
65 Window, Atom,
66 Lisp_Object, Atom));
67static Lisp_Object selection_data_to_lisp_data P_ ((Display *, unsigned char *,
68 int, Atom, int));
69static void lisp_data_to_selection_data P_ ((Display *, Lisp_Object,
70 unsigned char **, Atom *,
71 unsigned *, int *, int *));
72static Lisp_Object clean_local_selection_data P_ ((Lisp_Object));
73static void initialize_cut_buffers P_ ((Display *, Window));
74
75
76/* Printing traces to stderr. */
77
78#ifdef TRACE_SELECTION
79#define TRACE0(fmt) \
80 fprintf (stderr, "%d: " fmt "\n", getpid ())
81#define TRACE1(fmt, a0) \
82 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
83#define TRACE2(fmt, a0, a1) \
84 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
85#else
86#define TRACE0(fmt) (void) 0
87#define TRACE1(fmt, a0) (void) 0
88#define TRACE2(fmt, a0, a1) (void) 0
89#endif
90
91
ede4db72
RS
92#define CUT_BUFFER_SUPPORT
93
ede4db72
RS
94Lisp_Object QPRIMARY, QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
95 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
96 QATOM_PAIR;
97
e6c7c988 98Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
5109c8dd 99Lisp_Object QUTF8_STRING; /* This is a type of selection. */
e6c7c988 100
5a79ea57 101Lisp_Object Qcompound_text_with_extensions;
fbbe0ace 102
ede4db72
RS
103#ifdef CUT_BUFFER_SUPPORT
104Lisp_Object QCUT_BUFFER0, QCUT_BUFFER1, QCUT_BUFFER2, QCUT_BUFFER3,
105 QCUT_BUFFER4, QCUT_BUFFER5, QCUT_BUFFER6, QCUT_BUFFER7;
106#endif
107
8f4f023f
RS
108static Lisp_Object Vx_lost_selection_hooks;
109static Lisp_Object Vx_sent_selection_hooks;
2584c9ec
KH
110/* Coding system for communicating with other X clients via cutbuffer,
111 selection, and clipboard. */
93e4ce1b 112static Lisp_Object Vselection_coding_system;
ede4db72 113
16cd5029
KH
114/* Coding system for the next communicating with other X clients. */
115static Lisp_Object Vnext_selection_coding_system;
116
ede4db72
RS
117/* If this is a smaller number than the max-request-size of the display,
118 emacs will use INCR selection transfer when the selection is larger
119 than this. The max-request-size is usually around 64k, so if you want
1b65481e 120 emacs to use incremental selection transfers when the selection is
ede4db72 121 smaller than that, set this. I added this mostly for debugging the
8f4f023f 122 incremental transfer stuff, but it might improve server performance. */
ede4db72
RS
123#define MAX_SELECTION_QUANTUM 0xFFFFFF
124
c3498e64
JB
125#ifdef HAVE_X11R4
126#define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
127#else
128#define SELECTION_QUANTUM(dpy) (((dpy)->max_request_size << 2) - 100)
129#endif
ede4db72 130
7da64e5c 131/* The timestamp of the last input event Emacs received from the X server. */
29674445
KH
132/* Defined in keyboard.c. */
133extern unsigned long last_event_timestamp;
ede4db72
RS
134
135/* This is an association list whose elements are of the form
118bd841
RS
136 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
137 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
138 SELECTION-VALUE is the value that emacs owns for that selection.
ede4db72 139 It may be any kind of Lisp object.
118bd841 140 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
ede4db72 141 as a cons of two 16-bit numbers (making a 32 bit time.)
118bd841
RS
142 FRAME is the frame for which we made the selection.
143 If there is an entry in this alist, then it can be assumed that Emacs owns
ede4db72
RS
144 that selection.
145 The only (eq) parts of this list that are visible from Lisp are the
8f4f023f
RS
146 selection-values. */
147static Lisp_Object Vselection_alist;
ede4db72
RS
148
149/* This is an alist whose CARs are selection-types (whose names are the same
150 as the names of X Atoms) and whose CDRs are the names of Lisp functions to
1b65481e 151 call to convert the given Emacs selection value to a string representing
ede4db72 152 the given selection type. This is for Lisp-level extension of the emacs
8f4f023f
RS
153 selection handling. */
154static Lisp_Object Vselection_converter_alist;
ede4db72
RS
155
156/* If the selection owner takes too long to reply to a selection request,
8f4f023f 157 we give up on it. This is in milliseconds (0 = no timeout.) */
31ade731 158static EMACS_INT x_selection_timeout;
ede4db72
RS
159\f
160/* Utility functions */
161
162static void lisp_data_to_selection_data ();
163static Lisp_Object selection_data_to_lisp_data ();
164static Lisp_Object x_get_window_property_as_lisp_data ();
165
1b65481e 166/* This converts a Lisp symbol to a server Atom, avoiding a server
ede4db72
RS
167 roundtrip whenever possible. */
168
169static Atom
5c3a351a
RS
170symbol_to_x_atom (dpyinfo, display, sym)
171 struct x_display_info *dpyinfo;
ede4db72
RS
172 Display *display;
173 Lisp_Object sym;
174{
175 Atom val;
176 if (NILP (sym)) return 0;
177 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
178 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
179 if (EQ (sym, QSTRING)) return XA_STRING;
180 if (EQ (sym, QINTEGER)) return XA_INTEGER;
181 if (EQ (sym, QATOM)) return XA_ATOM;
5c3a351a
RS
182 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
183 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
184 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
e6c7c988 185 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
5109c8dd 186 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
5c3a351a
RS
187 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
188 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
189 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
190 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
191 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
192 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
ede4db72
RS
193#ifdef CUT_BUFFER_SUPPORT
194 if (EQ (sym, QCUT_BUFFER0)) return XA_CUT_BUFFER0;
195 if (EQ (sym, QCUT_BUFFER1)) return XA_CUT_BUFFER1;
196 if (EQ (sym, QCUT_BUFFER2)) return XA_CUT_BUFFER2;
197 if (EQ (sym, QCUT_BUFFER3)) return XA_CUT_BUFFER3;
198 if (EQ (sym, QCUT_BUFFER4)) return XA_CUT_BUFFER4;
199 if (EQ (sym, QCUT_BUFFER5)) return XA_CUT_BUFFER5;
200 if (EQ (sym, QCUT_BUFFER6)) return XA_CUT_BUFFER6;
201 if (EQ (sym, QCUT_BUFFER7)) return XA_CUT_BUFFER7;
202#endif
203 if (!SYMBOLP (sym)) abort ();
204
d5db4077 205 TRACE1 (" XInternAtom %s", (char *) SDATA (SYMBOL_NAME (sym)));
ede4db72 206 BLOCK_INPUT;
d5db4077 207 val = XInternAtom (display, (char *) SDATA (SYMBOL_NAME (sym)), False);
ede4db72
RS
208 UNBLOCK_INPUT;
209 return val;
210}
211
212
213/* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
214 and calls to intern whenever possible. */
215
216static Lisp_Object
d9c0d4a3
GM
217x_atom_to_symbol (dpy, atom)
218 Display *dpy;
ede4db72
RS
219 Atom atom;
220{
d9c0d4a3 221 struct x_display_info *dpyinfo;
ede4db72
RS
222 char *str;
223 Lisp_Object val;
1b65481e 224
d9c0d4a3
GM
225 if (! atom)
226 return Qnil;
1b65481e 227
7da64e5c
RS
228 switch (atom)
229 {
230 case XA_PRIMARY:
231 return QPRIMARY;
232 case XA_SECONDARY:
233 return QSECONDARY;
234 case XA_STRING:
235 return QSTRING;
236 case XA_INTEGER:
237 return QINTEGER;
238 case XA_ATOM:
239 return QATOM;
ede4db72 240#ifdef CUT_BUFFER_SUPPORT
7da64e5c
RS
241 case XA_CUT_BUFFER0:
242 return QCUT_BUFFER0;
243 case XA_CUT_BUFFER1:
244 return QCUT_BUFFER1;
245 case XA_CUT_BUFFER2:
246 return QCUT_BUFFER2;
247 case XA_CUT_BUFFER3:
248 return QCUT_BUFFER3;
249 case XA_CUT_BUFFER4:
250 return QCUT_BUFFER4;
251 case XA_CUT_BUFFER5:
252 return QCUT_BUFFER5;
253 case XA_CUT_BUFFER6:
254 return QCUT_BUFFER6;
255 case XA_CUT_BUFFER7:
256 return QCUT_BUFFER7;
ede4db72 257#endif
7da64e5c
RS
258 }
259
d9c0d4a3 260 dpyinfo = x_display_info_for_display (dpy);
5c3a351a 261 if (atom == dpyinfo->Xatom_CLIPBOARD)
7da64e5c 262 return QCLIPBOARD;
5c3a351a 263 if (atom == dpyinfo->Xatom_TIMESTAMP)
7da64e5c 264 return QTIMESTAMP;
5c3a351a 265 if (atom == dpyinfo->Xatom_TEXT)
7da64e5c 266 return QTEXT;
e6c7c988
KH
267 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
268 return QCOMPOUND_TEXT;
5109c8dd
KH
269 if (atom == dpyinfo->Xatom_UTF8_STRING)
270 return QUTF8_STRING;
5c3a351a 271 if (atom == dpyinfo->Xatom_DELETE)
7da64e5c 272 return QDELETE;
5c3a351a 273 if (atom == dpyinfo->Xatom_MULTIPLE)
7da64e5c 274 return QMULTIPLE;
5c3a351a 275 if (atom == dpyinfo->Xatom_INCR)
7da64e5c 276 return QINCR;
5c3a351a 277 if (atom == dpyinfo->Xatom_EMACS_TMP)
7da64e5c 278 return QEMACS_TMP;
5c3a351a 279 if (atom == dpyinfo->Xatom_TARGETS)
7da64e5c 280 return QTARGETS;
5c3a351a 281 if (atom == dpyinfo->Xatom_NULL)
7da64e5c 282 return QNULL;
ede4db72
RS
283
284 BLOCK_INPUT;
d9c0d4a3 285 str = XGetAtomName (dpy, atom);
ede4db72 286 UNBLOCK_INPUT;
d9c0d4a3 287 TRACE1 ("XGetAtomName --> %s", str);
ede4db72
RS
288 if (! str) return Qnil;
289 val = intern (str);
290 BLOCK_INPUT;
0158abbc 291 /* This was allocated by Xlib, so use XFree. */
ede4db72
RS
292 XFree (str);
293 UNBLOCK_INPUT;
294 return val;
295}
8a89415e 296\f
ede4db72 297/* Do protocol to assert ourself as a selection owner.
1b65481e 298 Update the Vselection_alist so that we can reply to later requests for
ede4db72
RS
299 our selection. */
300
301static void
302x_own_selection (selection_name, selection_value)
303 Lisp_Object selection_name, selection_value;
304{
378c33ca
GM
305 struct frame *sf = SELECTED_FRAME ();
306 Window selecting_window = FRAME_X_WINDOW (sf);
307 Display *display = FRAME_X_DISPLAY (sf);
7da64e5c 308 Time time = last_event_timestamp;
ede4db72 309 Atom selection_atom;
378c33ca 310 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
5a499696 311 int count;
ede4db72 312
b7826503 313 CHECK_SYMBOL (selection_name);
5c3a351a 314 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
ede4db72
RS
315
316 BLOCK_INPUT;
5a499696 317 count = x_catch_errors (display);
ede4db72 318 XSetSelectionOwner (display, selection_atom, selecting_window, time);
a7b24d46 319 x_check_errors (display, "Can't set selection: %s");
5a499696 320 x_uncatch_errors (display, count);
ede4db72
RS
321 UNBLOCK_INPUT;
322
323 /* Now update the local cache */
324 {
325 Lisp_Object selection_time;
326 Lisp_Object selection_data;
327 Lisp_Object prev_value;
328
329 selection_time = long_to_cons ((unsigned long) time);
330 selection_data = Fcons (selection_name,
331 Fcons (selection_value,
118bd841 332 Fcons (selection_time,
378c33ca 333 Fcons (selected_frame, Qnil))));
ede4db72
RS
334 prev_value = assq_no_quit (selection_name, Vselection_alist);
335
336 Vselection_alist = Fcons (selection_data, Vselection_alist);
337
338 /* If we already owned the selection, remove the old selection data.
339 Perhaps we should destructively modify it instead.
340 Don't use Fdelq as that may QUIT. */
341 if (!NILP (prev_value))
342 {
343 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
344 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
8e713be6 345 if (EQ (prev_value, Fcar (XCDR (rest))))
ede4db72 346 {
f3fbd155 347 XSETCDR (rest, Fcdr (XCDR (rest)));
ede4db72
RS
348 break;
349 }
350 }
351 }
352}
353\f
354/* Given a selection-name and desired type, look up our local copy of
355 the selection value and convert it to the type.
356 The value is nil or a string.
5109c8dd
KH
357 This function is used both for remote requests (LOCAL_REQUEST is zero)
358 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
ede4db72
RS
359
360 This calls random Lisp code, and may signal or gc. */
361
362static Lisp_Object
5109c8dd 363x_get_local_selection (selection_symbol, target_type, local_request)
ede4db72 364 Lisp_Object selection_symbol, target_type;
5109c8dd 365 int local_request;
ede4db72
RS
366{
367 Lisp_Object local_value;
368 Lisp_Object handler_fn, value, type, check;
369 int count;
370
371 local_value = assq_no_quit (selection_symbol, Vselection_alist);
372
373 if (NILP (local_value)) return Qnil;
374
375 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
376 if (EQ (target_type, QTIMESTAMP))
377 {
378 handler_fn = Qnil;
8e713be6 379 value = XCAR (XCDR (XCDR (local_value)));
ede4db72
RS
380 }
381#if 0
382 else if (EQ (target_type, QDELETE))
383 {
384 handler_fn = Qnil;
385 Fx_disown_selection_internal
386 (selection_symbol,
8e713be6 387 XCAR (XCDR (XCDR (local_value))));
ede4db72
RS
388 value = QNULL;
389 }
390#endif
391
392#if 0 /* #### MULTIPLE doesn't work yet */
393 else if (CONSP (target_type)
8e713be6 394 && XCAR (target_type) == QMULTIPLE)
ede4db72 395 {
9d2d1dd8
KH
396 Lisp_Object pairs;
397 int size;
ede4db72 398 int i;
8e713be6 399 pairs = XCDR (target_type);
9d2d1dd8 400 size = XVECTOR (pairs)->size;
ede4db72
RS
401 /* If the target is MULTIPLE, then target_type looks like
402 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
403 We modify the second element of each pair in the vector and
404 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
405 */
406 for (i = 0; i < size; i++)
407 {
9d2d1dd8
KH
408 Lisp_Object pair;
409 pair = XVECTOR (pairs)->contents [i];
ede4db72
RS
410 XVECTOR (pair)->contents [1]
411 = x_get_local_selection (XVECTOR (pair)->contents [0],
5109c8dd
KH
412 XVECTOR (pair)->contents [1],
413 local_request);
ede4db72
RS
414 }
415 return pairs;
416 }
417#endif
418 else
419 {
420 /* Don't allow a quit within the converter.
421 When the user types C-g, he would be surprised
422 if by luck it came during a converter. */
aed13378 423 count = SPECPDL_INDEX ();
ede4db72
RS
424 specbind (Qinhibit_quit, Qt);
425
b7826503 426 CHECK_SYMBOL (target_type);
ede4db72 427 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
1eb4d468
RS
428 if (!NILP (handler_fn))
429 value = call3 (handler_fn,
5109c8dd 430 selection_symbol, (local_request ? Qnil : target_type),
8e713be6 431 XCAR (XCDR (local_value)));
1eb4d468
RS
432 else
433 value = Qnil;
ede4db72
RS
434 unbind_to (count, Qnil);
435 }
436
437 /* Make sure this value is of a type that we could transmit
438 to another X client. */
a87ed99c 439
ede4db72
RS
440 check = value;
441 if (CONSP (value)
8e713be6
KR
442 && SYMBOLP (XCAR (value)))
443 type = XCAR (value),
444 check = XCDR (value);
1b65481e 445
ede4db72
RS
446 if (STRINGP (check)
447 || VECTORP (check)
448 || SYMBOLP (check)
7da64e5c 449 || INTEGERP (check)
ede4db72
RS
450 || NILP (value))
451 return value;
a87ed99c 452 /* Check for a value that cons_to_long could handle. */
ede4db72 453 else if (CONSP (check)
8e713be6
KR
454 && INTEGERP (XCAR (check))
455 && (INTEGERP (XCDR (check))
ede4db72 456 ||
8e713be6
KR
457 (CONSP (XCDR (check))
458 && INTEGERP (XCAR (XCDR (check)))
459 && NILP (XCDR (XCDR (check))))))
ede4db72
RS
460 return value;
461 else
462 return
463 Fsignal (Qerror,
a87ed99c 464 Fcons (build_string ("invalid data returned by selection-conversion function"),
ede4db72
RS
465 Fcons (handler_fn, Fcons (value, Qnil))));
466}
467\f
468/* Subroutines of x_reply_selection_request. */
469
5d0ba25b 470/* Send a SelectionNotify event to the requestor with property=None,
ede4db72
RS
471 meaning we were unable to do what they wanted. */
472
473static void
474x_decline_selection_request (event)
475 struct input_event *event;
476{
477 XSelectionEvent reply;
d9c0d4a3 478 int count;
1b65481e 479
ede4db72
RS
480 reply.type = SelectionNotify;
481 reply.display = SELECTION_EVENT_DISPLAY (event);
5d0ba25b 482 reply.requestor = SELECTION_EVENT_REQUESTOR (event);
ede4db72
RS
483 reply.selection = SELECTION_EVENT_SELECTION (event);
484 reply.time = SELECTION_EVENT_TIME (event);
485 reply.target = SELECTION_EVENT_TARGET (event);
486 reply.property = None;
487
d9c0d4a3
GM
488 /* The reason for the error may be that the receiver has
489 died in the meantime. Handle that case. */
ede4db72 490 BLOCK_INPUT;
d9c0d4a3
GM
491 count = x_catch_errors (reply.display);
492 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
5c3a351a 493 XFlush (reply.display);
d9c0d4a3 494 x_uncatch_errors (reply.display, count);
ede4db72
RS
495 UNBLOCK_INPUT;
496}
497
498/* This is the selection request currently being processed.
499 It is set to zero when the request is fully processed. */
500static struct input_event *x_selection_current_request;
501
ca29f2b8
GM
502/* Display info in x_selection_request. */
503
504static struct x_display_info *selection_request_dpyinfo;
505
ede4db72 506/* Used as an unwind-protect clause so that, if a selection-converter signals
2a1a4c9d 507 an error, we tell the requester that we were unable to do what they wanted
ede4db72
RS
508 before we throw to top-level or go into the debugger or whatever. */
509
510static Lisp_Object
511x_selection_request_lisp_error (ignore)
512 Lisp_Object ignore;
513{
ca29f2b8
GM
514 if (x_selection_current_request != 0
515 && selection_request_dpyinfo->display)
ede4db72
RS
516 x_decline_selection_request (x_selection_current_request);
517 return Qnil;
518}
519\f
d1f21a66
RS
520
521/* This stuff is so that INCR selections are reentrant (that is, so we can
522 be servicing multiple INCR selection requests simultaneously.) I haven't
523 actually tested that yet. */
524
525/* Keep a list of the property changes that are awaited. */
526
527struct prop_location
528{
529 int identifier;
530 Display *display;
531 Window window;
532 Atom property;
533 int desired_state;
534 int arrived;
535 struct prop_location *next;
536};
537
538static struct prop_location *expect_property_change ();
539static void wait_for_property_change ();
540static void unexpect_property_change ();
541static int waiting_for_other_props_on_window ();
542
543static int prop_location_identifier;
544
545static Lisp_Object property_change_reply;
546
547static struct prop_location *property_change_reply_object;
548
549static struct prop_location *property_change_wait_list;
55b2d45d
RS
550
551static Lisp_Object
552queue_selection_requests_unwind (frame)
553 Lisp_Object frame;
554{
555 FRAME_PTR f = XFRAME (frame);
556
557 if (! NILP (frame))
558 x_stop_queuing_selection_requests (FRAME_X_DISPLAY (f));
ab552306 559 return Qnil;
55b2d45d
RS
560}
561
562/* Return some frame whose display info is DPYINFO.
563 Return nil if there is none. */
564
565static Lisp_Object
566some_frame_on_display (dpyinfo)
567 struct x_display_info *dpyinfo;
568{
569 Lisp_Object list, frame;
570
571 FOR_EACH_FRAME (list, frame)
572 {
573 if (FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
574 return frame;
575 }
576
577 return Qnil;
578}
d1f21a66 579\f
ede4db72
RS
580/* Send the reply to a selection request event EVENT.
581 TYPE is the type of selection data requested.
582 DATA and SIZE describe the data to send, already converted.
583 FORMAT is the unit-size (in bits) of the data to be transmitted. */
584
585static void
586x_reply_selection_request (event, format, data, size, type)
587 struct input_event *event;
588 int format, size;
589 unsigned char *data;
590 Atom type;
591{
592 XSelectionEvent reply;
593 Display *display = SELECTION_EVENT_DISPLAY (event);
5d0ba25b 594 Window window = SELECTION_EVENT_REQUESTOR (event);
ede4db72
RS
595 int bytes_remaining;
596 int format_bytes = format/8;
597 int max_bytes = SELECTION_QUANTUM (display);
5c3a351a 598 struct x_display_info *dpyinfo = x_display_info_for_display (display);
5a499696 599 int count;
ede4db72
RS
600
601 if (max_bytes > MAX_SELECTION_QUANTUM)
602 max_bytes = MAX_SELECTION_QUANTUM;
603
604 reply.type = SelectionNotify;
605 reply.display = display;
5d0ba25b 606 reply.requestor = window;
ede4db72
RS
607 reply.selection = SELECTION_EVENT_SELECTION (event);
608 reply.time = SELECTION_EVENT_TIME (event);
609 reply.target = SELECTION_EVENT_TARGET (event);
610 reply.property = SELECTION_EVENT_PROPERTY (event);
611 if (reply.property == None)
612 reply.property = reply.target;
613
614 /* #### XChangeProperty can generate BadAlloc, and we must handle it! */
afe1529d 615 BLOCK_INPUT;
ad7f9d12 616 count = x_catch_errors (display);
ede4db72 617
ede4db72
RS
618 /* Store the data on the requested property.
619 If the selection is large, only store the first N bytes of it.
620 */
621 bytes_remaining = size * format_bytes;
622 if (bytes_remaining <= max_bytes)
623 {
624 /* Send all the data at once, with minimal handshaking. */
d9c0d4a3 625 TRACE1 ("Sending all %d bytes", bytes_remaining);
ede4db72
RS
626 XChangeProperty (display, window, reply.property, type, format,
627 PropModeReplace, data, size);
628 /* At this point, the selection was successfully stored; ack it. */
2f65feb6 629 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
ede4db72
RS
630 }
631 else
632 {
633 /* Send an INCR selection. */
d1f21a66 634 struct prop_location *wait_object;
afe1529d 635 int had_errors;
55b2d45d 636 Lisp_Object frame;
ede4db72 637
55b2d45d
RS
638 frame = some_frame_on_display (dpyinfo);
639
640 /* If the display no longer has frames, we can't expect
641 to get many more selection requests from it, so don't
642 bother trying to queue them. */
643 if (!NILP (frame))
644 {
645 x_start_queuing_selection_requests (display);
646
647 record_unwind_protect (queue_selection_requests_unwind,
648 frame);
649 }
2f65feb6 650
19126e11 651 if (x_window_to_frame (dpyinfo, window)) /* #### debug */
606140dd 652 error ("Attempt to transfer an INCR to ourself!");
1b65481e 653
d9c0d4a3
GM
654 TRACE2 ("Start sending %d bytes incrementally (%s)",
655 bytes_remaining, XGetAtomName (display, reply.property));
d1f21a66
RS
656 wait_object = expect_property_change (display, window, reply.property,
657 PropertyDelete);
ede4db72 658
d9c0d4a3
GM
659 TRACE1 ("Set %s to number of bytes to send",
660 XGetAtomName (display, reply.property));
5c3a351a 661 XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
55b2d45d
RS
662 32, PropModeReplace,
663 (unsigned char *) &bytes_remaining, 1);
ede4db72 664 XSelectInput (display, window, PropertyChangeMask);
1b65481e 665
ede4db72 666 /* Tell 'em the INCR data is there... */
d9c0d4a3 667 TRACE0 ("Send SelectionNotify event");
55b2d45d 668 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
5c3a351a 669 XFlush (display);
afe1529d
RS
670
671 had_errors = x_had_errors_p (display);
2f65feb6 672 UNBLOCK_INPUT;
ede4db72 673
2a1a4c9d 674 /* First, wait for the requester to ack by deleting the property.
ede4db72 675 This can run random lisp code (process handlers) or signal. */
afe1529d 676 if (! had_errors)
d9c0d4a3
GM
677 {
678 TRACE1 ("Waiting for ACK (deletion of %s)",
679 XGetAtomName (display, reply.property));
680 wait_for_property_change (wait_object);
681 }
ede4db72 682
d9c0d4a3 683 TRACE0 ("Got ACK");
ede4db72
RS
684 while (bytes_remaining)
685 {
686 int i = ((bytes_remaining < max_bytes)
687 ? bytes_remaining
688 : max_bytes);
2f65feb6
RS
689
690 BLOCK_INPUT;
691
d1f21a66
RS
692 wait_object
693 = expect_property_change (display, window, reply.property,
694 PropertyDelete);
d9c0d4a3
GM
695
696 TRACE1 ("Sending increment of %d bytes", i);
697 TRACE1 ("Set %s to increment data",
698 XGetAtomName (display, reply.property));
1b65481e 699
ede4db72
RS
700 /* Append the next chunk of data to the property. */
701 XChangeProperty (display, window, reply.property, type, format,
702 PropModeAppend, data, i / format_bytes);
703 bytes_remaining -= i;
704 data += i;
5c3a351a 705 XFlush (display);
afe1529d 706 had_errors = x_had_errors_p (display);
2f65feb6 707 UNBLOCK_INPUT;
ede4db72 708
afe1529d
RS
709 if (had_errors)
710 break;
711
2a1a4c9d 712 /* Now wait for the requester to ack this chunk by deleting the
d9c0d4a3
GM
713 property. This can run random lisp code or signal. */
714 TRACE1 ("Waiting for increment ACK (deletion of %s)",
715 XGetAtomName (display, reply.property));
d1f21a66 716 wait_for_property_change (wait_object);
ede4db72 717 }
1b65481e 718
d9c0d4a3
GM
719 /* Now write a zero-length chunk to the property to tell the
720 requester that we're done. */
2f65feb6 721 BLOCK_INPUT;
ede4db72
RS
722 if (! waiting_for_other_props_on_window (display, window))
723 XSelectInput (display, window, 0L);
724
d9c0d4a3
GM
725 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
726 XGetAtomName (display, reply.property));
ede4db72
RS
727 XChangeProperty (display, window, reply.property, type, format,
728 PropModeReplace, data, 0);
d9c0d4a3 729 TRACE0 ("Done sending incrementally");
ede4db72 730 }
afe1529d 731
47a6ac17
GM
732 /* The window we're communicating with may have been deleted
733 in the meantime (that's a real situation from a bug report).
734 In this case, there may be events in the event queue still
735 refering to the deleted window, and we'll get a BadWindow error
736 in XTread_socket when processing the events. I don't have
737 an idea how to fix that. gerd, 2001-01-98. */
afe1529d 738 XFlush (display);
5a499696 739 x_uncatch_errors (display, count);
afe1529d 740 UNBLOCK_INPUT;
ede4db72
RS
741}
742\f
743/* Handle a SelectionRequest event EVENT.
744 This is called from keyboard.c when such an event is found in the queue. */
745
746void
747x_handle_selection_request (event)
748 struct input_event *event;
749{
750 struct gcpro gcpro1, gcpro2, gcpro3;
9d2d1dd8 751 Lisp_Object local_selection_data;
ede4db72 752 Lisp_Object selection_symbol;
9d2d1dd8
KH
753 Lisp_Object target_symbol;
754 Lisp_Object converted_selection;
ede4db72 755 Time local_selection_time;
9d2d1dd8 756 Lisp_Object successful_p;
ede4db72 757 int count;
5c3a351a
RS
758 struct x_display_info *dpyinfo
759 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
ede4db72 760
9d2d1dd8
KH
761 local_selection_data = Qnil;
762 target_symbol = Qnil;
763 converted_selection = Qnil;
764 successful_p = Qnil;
765
ede4db72
RS
766 GCPRO3 (local_selection_data, converted_selection, target_symbol);
767
d9c0d4a3 768 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
ede4db72
RS
769 SELECTION_EVENT_SELECTION (event));
770
771 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
ede4db72
RS
772
773 if (NILP (local_selection_data))
774 {
775 /* Someone asked for the selection, but we don't have it any more.
776 */
777 x_decline_selection_request (event);
778 goto DONE;
779 }
780
781 local_selection_time = (Time)
8e713be6 782 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
ede4db72
RS
783
784 if (SELECTION_EVENT_TIME (event) != CurrentTime
7da64e5c 785 && local_selection_time > SELECTION_EVENT_TIME (event))
ede4db72
RS
786 {
787 /* Someone asked for the selection, and we have one, but not the one
788 they're looking for.
789 */
790 x_decline_selection_request (event);
791 goto DONE;
792 }
793
ede4db72 794 x_selection_current_request = event;
331379bf 795 count = SPECPDL_INDEX ();
ca29f2b8 796 selection_request_dpyinfo = dpyinfo;
ede4db72
RS
797 record_unwind_protect (x_selection_request_lisp_error, Qnil);
798
d9c0d4a3 799 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
ede4db72
RS
800 SELECTION_EVENT_TARGET (event));
801
802#if 0 /* #### MULTIPLE doesn't work yet */
803 if (EQ (target_symbol, QMULTIPLE))
804 target_symbol = fetch_multiple_target (event);
805#endif
1b65481e 806
ede4db72 807 /* Convert lisp objects back into binary data */
1b65481e 808
ede4db72 809 converted_selection
5109c8dd 810 = x_get_local_selection (selection_symbol, target_symbol, 0);
1b65481e 811
ede4db72
RS
812 if (! NILP (converted_selection))
813 {
814 unsigned char *data;
815 unsigned int size;
816 int format;
817 Atom type;
aca39f42
RS
818 int nofree;
819
2f65feb6
RS
820 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
821 converted_selection,
aca39f42 822 &data, &type, &size, &format, &nofree);
1b65481e 823
ede4db72
RS
824 x_reply_selection_request (event, format, data, size, type);
825 successful_p = Qt;
826
827 /* Indicate we have successfully processed this event. */
7da64e5c 828 x_selection_current_request = 0;
ede4db72 829
4feb31b2 830 /* Use xfree, not XFree, because lisp_data_to_selection_data
0158abbc 831 calls xmalloc itself. */
aca39f42 832 if (!nofree)
4feb31b2 833 xfree (data);
ede4db72
RS
834 }
835 unbind_to (count, Qnil);
836
837 DONE:
838
839 UNGCPRO;
840
841 /* Let random lisp code notice that the selection has been asked for. */
842 {
9d2d1dd8
KH
843 Lisp_Object rest;
844 rest = Vx_sent_selection_hooks;
ede4db72
RS
845 if (!EQ (rest, Qunbound))
846 for (; CONSP (rest); rest = Fcdr (rest))
847 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
848 }
849}
850\f
e18e6130 851/* Handle a SelectionClear event EVENT, which indicates that some
ede4db72
RS
852 client cleared out our previously asserted selection.
853 This is called from keyboard.c when such an event is found in the queue. */
854
855void
856x_handle_selection_clear (event)
857 struct input_event *event;
858{
859 Display *display = SELECTION_EVENT_DISPLAY (event);
860 Atom selection = SELECTION_EVENT_SELECTION (event);
861 Time changed_owner_time = SELECTION_EVENT_TIME (event);
1b65481e 862
ede4db72
RS
863 Lisp_Object selection_symbol, local_selection_data;
864 Time local_selection_time;
5c3a351a 865 struct x_display_info *dpyinfo = x_display_info_for_display (display);
e18e6130
RS
866 struct x_display_info *t_dpyinfo;
867
868 /* If the new selection owner is also Emacs,
869 don't clear the new selection. */
870 BLOCK_INPUT;
871 /* Check each display on the same terminal,
872 to see if this Emacs job now owns the selection
873 through that display. */
874 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
875 if (t_dpyinfo->kboard == dpyinfo->kboard)
876 {
877 Window owner_window
878 = XGetSelectionOwner (t_dpyinfo->display, selection);
879 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
880 {
881 UNBLOCK_INPUT;
882 return;
883 }
884 }
885 UNBLOCK_INPUT;
ede4db72 886
d9c0d4a3 887 selection_symbol = x_atom_to_symbol (display, selection);
ede4db72
RS
888
889 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
890
891 /* Well, we already believe that we don't own it, so that's just fine. */
892 if (NILP (local_selection_data)) return;
893
894 local_selection_time = (Time)
8e713be6 895 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
ede4db72
RS
896
897 /* This SelectionClear is for a selection that we no longer own, so we can
898 disregard it. (That is, we have reasserted the selection since this
899 request was generated.) */
900
901 if (changed_owner_time != CurrentTime
902 && local_selection_time > changed_owner_time)
903 return;
904
905 /* Otherwise, we're really honest and truly being told to drop it.
906 Don't use Fdelq as that may QUIT;. */
907
908 if (EQ (local_selection_data, Fcar (Vselection_alist)))
909 Vselection_alist = Fcdr (Vselection_alist);
910 else
911 {
912 Lisp_Object rest;
913 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
8e713be6 914 if (EQ (local_selection_data, Fcar (XCDR (rest))))
ede4db72 915 {
f3fbd155 916 XSETCDR (rest, Fcdr (XCDR (rest)));
ede4db72
RS
917 break;
918 }
919 }
920
921 /* Let random lisp code notice that the selection has been stolen. */
922
923 {
d1f21a66
RS
924 Lisp_Object rest;
925 rest = Vx_lost_selection_hooks;
ede4db72 926 if (!EQ (rest, Qunbound))
d1f21a66
RS
927 {
928 for (; CONSP (rest); rest = Fcdr (rest))
929 call1 (Fcar (rest), selection_symbol);
7c6b2ea4 930 prepare_menu_bars ();
3007ebfb 931 redisplay_preserve_echo_area (20);
d1f21a66 932 }
ede4db72
RS
933 }
934}
935
118bd841
RS
936/* Clear all selections that were made from frame F.
937 We do this when about to delete a frame. */
938
939void
940x_clear_frame_selections (f)
941 FRAME_PTR f;
942{
943 Lisp_Object frame;
944 Lisp_Object rest;
945
90851bbe 946 XSETFRAME (frame, f);
118bd841
RS
947
948 /* Otherwise, we're really honest and truly being told to drop it.
949 Don't use Fdelq as that may QUIT;. */
950
0d199f9c
RS
951 /* Delete elements from the beginning of Vselection_alist. */
952 while (!NILP (Vselection_alist)
953 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
954 {
955 /* Let random Lisp code notice that the selection has been stolen. */
956 Lisp_Object hooks, selection_symbol;
957
958 hooks = Vx_lost_selection_hooks;
959 selection_symbol = Fcar (Fcar (Vselection_alist));
960
961 if (!EQ (hooks, Qunbound))
962 {
963 for (; CONSP (hooks); hooks = Fcdr (hooks))
964 call1 (Fcar (hooks), selection_symbol);
996b804d
MB
965#if 0 /* This can crash when deleting a frame
966 from x_connection_closed. Anyway, it seems unnecessary;
967 something else should cause a redisplay. */
3007ebfb 968 redisplay_preserve_echo_area (21);
996b804d 969#endif
0d199f9c
RS
970 }
971
972 Vselection_alist = Fcdr (Vselection_alist);
973 }
974
975 /* Delete elements after the beginning of Vselection_alist. */
118bd841 976 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
8e713be6 977 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
118bd841
RS
978 {
979 /* Let random Lisp code notice that the selection has been stolen. */
980 Lisp_Object hooks, selection_symbol;
981
982 hooks = Vx_lost_selection_hooks;
8e713be6 983 selection_symbol = Fcar (Fcar (XCDR (rest)));
118bd841
RS
984
985 if (!EQ (hooks, Qunbound))
986 {
987 for (; CONSP (hooks); hooks = Fcdr (hooks))
988 call1 (Fcar (hooks), selection_symbol);
996b804d 989#if 0 /* See above */
3007ebfb 990 redisplay_preserve_echo_area (22);
996b804d 991#endif
118bd841 992 }
f3fbd155 993 XSETCDR (rest, Fcdr (XCDR (rest)));
118bd841
RS
994 break;
995 }
996}
ede4db72 997\f
ede4db72
RS
998/* Nonzero if any properties for DISPLAY and WINDOW
999 are on the list of what we are waiting for. */
1000
1001static int
1002waiting_for_other_props_on_window (display, window)
1003 Display *display;
1004 Window window;
1005{
1006 struct prop_location *rest = property_change_wait_list;
1007 while (rest)
1008 if (rest->display == display && rest->window == window)
1009 return 1;
1010 else
1011 rest = rest->next;
1012 return 0;
1013}
1014
1015/* Add an entry to the list of property changes we are waiting for.
1016 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1017 The return value is a number that uniquely identifies
1018 this awaited property change. */
1019
d1f21a66 1020static struct prop_location *
ede4db72
RS
1021expect_property_change (display, window, property, state)
1022 Display *display;
1023 Window window;
6c070502 1024 Atom property;
ede4db72
RS
1025 int state;
1026{
d9c0d4a3 1027 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
2f65feb6 1028 pl->identifier = ++prop_location_identifier;
ede4db72
RS
1029 pl->display = display;
1030 pl->window = window;
1031 pl->property = property;
1032 pl->desired_state = state;
1033 pl->next = property_change_wait_list;
d1f21a66 1034 pl->arrived = 0;
ede4db72 1035 property_change_wait_list = pl;
d1f21a66 1036 return pl;
ede4db72
RS
1037}
1038
1039/* Delete an entry from the list of property changes we are waiting for.
2f65feb6 1040 IDENTIFIER is the number that uniquely identifies the entry. */
ede4db72
RS
1041
1042static void
d1f21a66
RS
1043unexpect_property_change (location)
1044 struct prop_location *location;
ede4db72
RS
1045{
1046 struct prop_location *prev = 0, *rest = property_change_wait_list;
1047 while (rest)
1048 {
d1f21a66 1049 if (rest == location)
ede4db72
RS
1050 {
1051 if (prev)
1052 prev->next = rest->next;
1053 else
1054 property_change_wait_list = rest->next;
4feb31b2 1055 xfree (rest);
ede4db72
RS
1056 return;
1057 }
1058 prev = rest;
1059 rest = rest->next;
1060 }
1061}
1062
2f65feb6
RS
1063/* Remove the property change expectation element for IDENTIFIER. */
1064
1065static Lisp_Object
1066wait_for_property_change_unwind (identifierval)
1067 Lisp_Object identifierval;
1068{
c4898489 1069 unexpect_property_change ((struct prop_location *)
8e713be6
KR
1070 (XFASTINT (XCAR (identifierval)) << 16
1071 | XFASTINT (XCDR (identifierval))));
ab552306 1072 return Qnil;
2f65feb6
RS
1073}
1074
ede4db72 1075/* Actually wait for a property change.
2f65feb6 1076 IDENTIFIER should be the value that expect_property_change returned. */
ede4db72
RS
1077
1078static void
d1f21a66
RS
1079wait_for_property_change (location)
1080 struct prop_location *location;
ede4db72 1081{
2f65feb6 1082 int secs, usecs;
aed13378 1083 int count = SPECPDL_INDEX ();
d1f21a66
RS
1084 Lisp_Object tem;
1085
c4898489 1086 tem = Fcons (Qnil, Qnil);
f3fbd155
KR
1087 XSETCARFASTINT (tem, (EMACS_UINT)location >> 16);
1088 XSETCDRFASTINT (tem, (EMACS_UINT)location & 0xffff);
2f65feb6
RS
1089
1090 /* Make sure to do unexpect_property_change if we quit or err. */
d1f21a66 1091 record_unwind_protect (wait_for_property_change_unwind, tem);
2f65feb6 1092
f3fbd155 1093 XSETCAR (property_change_reply, Qnil);
2f65feb6 1094
afe1529d
RS
1095 property_change_reply_object = location;
1096 /* If the event we are waiting for arrives beyond here, it will set
1097 property_change_reply, because property_change_reply_object says so. */
d1f21a66
RS
1098 if (! location->arrived)
1099 {
d1f21a66
RS
1100 secs = x_selection_timeout / 1000;
1101 usecs = (x_selection_timeout % 1000) * 1000;
d9c0d4a3 1102 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
d1f21a66
RS
1103 wait_reading_process_input (secs, usecs, property_change_reply, 0);
1104
8e713be6 1105 if (NILP (XCAR (property_change_reply)))
d9c0d4a3
GM
1106 {
1107 TRACE0 (" Timed out");
1108 error ("Timed out waiting for property-notify event");
1109 }
d1f21a66 1110 }
2f65feb6
RS
1111
1112 unbind_to (count, Qnil);
ede4db72
RS
1113}
1114
1115/* Called from XTread_socket in response to a PropertyNotify event. */
1116
1117void
1118x_handle_property_notify (event)
1119 XPropertyEvent *event;
1120{
1121 struct prop_location *prev = 0, *rest = property_change_wait_list;
d9c0d4a3 1122
ede4db72
RS
1123 while (rest)
1124 {
1125 if (rest->property == event->atom
1126 && rest->window == event->window
1127 && rest->display == event->display
1128 && rest->desired_state == event->state)
1129 {
d9c0d4a3
GM
1130 TRACE2 ("Expected %s of property %s",
1131 (event->state == PropertyDelete ? "deletion" : "change"),
1132 XGetAtomName (event->display, event->atom));
ede4db72 1133
d1f21a66
RS
1134 rest->arrived = 1;
1135
ede4db72
RS
1136 /* If this is the one wait_for_property_change is waiting for,
1137 tell it to wake up. */
d1f21a66 1138 if (rest == property_change_reply_object)
f3fbd155 1139 XSETCAR (property_change_reply, Qt);
ede4db72
RS
1140
1141 if (prev)
1142 prev->next = rest->next;
1143 else
1144 property_change_wait_list = rest->next;
4feb31b2 1145 xfree (rest);
ede4db72
RS
1146 return;
1147 }
1b65481e 1148
ede4db72
RS
1149 prev = rest;
1150 rest = rest->next;
1151 }
ede4db72
RS
1152}
1153
1154
1155\f
1156#if 0 /* #### MULTIPLE doesn't work yet */
1157
1158static Lisp_Object
1159fetch_multiple_target (event)
1160 XSelectionRequestEvent *event;
1161{
1162 Display *display = event->display;
5d0ba25b 1163 Window window = event->requestor;
ede4db72
RS
1164 Atom target = event->target;
1165 Atom selection_atom = event->selection;
1166 int result;
1167
1168 return
1169 Fcons (QMULTIPLE,
1170 x_get_window_property_as_lisp_data (display, window, target,
1171 QMULTIPLE, selection_atom));
1172}
1173
1174static Lisp_Object
1175copy_multiple_data (obj)
1176 Lisp_Object obj;
1177{
1178 Lisp_Object vec;
1179 int i;
1180 int size;
1181 if (CONSP (obj))
8e713be6 1182 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1b65481e 1183
b7826503 1184 CHECK_VECTOR (obj);
ede4db72
RS
1185 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1186 for (i = 0; i < size; i++)
1187 {
1188 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
b7826503 1189 CHECK_VECTOR (vec2);
ede4db72
RS
1190 if (XVECTOR (vec2)->size != 2)
1191 /* ??? Confusing error message */
1192 Fsignal (Qerror, Fcons (build_string ("vectors must be of length 2"),
1193 Fcons (vec2, Qnil)));
1194 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1195 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1196 = XVECTOR (vec2)->contents [0];
1197 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1198 = XVECTOR (vec2)->contents [1];
1199 }
1200 return vec;
1201}
1202
1203#endif
1204
1205\f
1206/* Variables for communication with x_handle_selection_notify. */
1207static Atom reading_which_selection;
1208static Lisp_Object reading_selection_reply;
1209static Window reading_selection_window;
1210
1211/* Do protocol to read selection-data from the server.
1212 Converts this to Lisp data and returns it. */
1213
1214static Lisp_Object
1215x_get_foreign_selection (selection_symbol, target_type)
1216 Lisp_Object selection_symbol, target_type;
1217{
378c33ca
GM
1218 struct frame *sf = SELECTED_FRAME ();
1219 Window requestor_window = FRAME_X_WINDOW (sf);
1220 Display *display = FRAME_X_DISPLAY (sf);
1221 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
5d0ba25b 1222 Time requestor_time = last_event_timestamp;
5c3a351a
RS
1223 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1224 Atom selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
ede4db72 1225 Atom type_atom;
80da0190 1226 int secs, usecs;
5a499696 1227 int count;
55b2d45d 1228 Lisp_Object frame;
ede4db72
RS
1229
1230 if (CONSP (target_type))
8e713be6 1231 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
ede4db72 1232 else
5c3a351a 1233 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
ede4db72
RS
1234
1235 BLOCK_INPUT;
1b65481e 1236
5a499696 1237 count = x_catch_errors (display);
1b65481e 1238
d9c0d4a3
GM
1239 TRACE2 ("Get selection %s, type %s",
1240 XGetAtomName (display, type_atom),
1241 XGetAtomName (display, target_property));
1242
ede4db72 1243 XConvertSelection (display, selection_atom, type_atom, target_property,
5d0ba25b 1244 requestor_window, requestor_time);
5c3a351a 1245 XFlush (display);
ede4db72
RS
1246
1247 /* Prepare to block until the reply has been read. */
5d0ba25b 1248 reading_selection_window = requestor_window;
ede4db72 1249 reading_which_selection = selection_atom;
f3fbd155 1250 XSETCAR (reading_selection_reply, Qnil);
55b2d45d
RS
1251
1252 frame = some_frame_on_display (dpyinfo);
1253
1254 /* If the display no longer has frames, we can't expect
1255 to get many more selection requests from it, so don't
1256 bother trying to queue them. */
1257 if (!NILP (frame))
1258 {
1259 x_start_queuing_selection_requests (display);
1260
1261 record_unwind_protect (queue_selection_requests_unwind,
1262 frame);
1263 }
ede4db72
RS
1264 UNBLOCK_INPUT;
1265
80da0190
RS
1266 /* This allows quits. Also, don't wait forever. */
1267 secs = x_selection_timeout / 1000;
1268 usecs = (x_selection_timeout % 1000) * 1000;
d9c0d4a3 1269 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
80da0190 1270 wait_reading_process_input (secs, usecs, reading_selection_reply, 0);
d9c0d4a3 1271 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
ede4db72 1272
7c6b2ea4 1273 BLOCK_INPUT;
a7b24d46 1274 x_check_errors (display, "Cannot get selection: %s");
5a499696 1275 x_uncatch_errors (display, count);
7c6b2ea4
RS
1276 UNBLOCK_INPUT;
1277
8e713be6 1278 if (NILP (XCAR (reading_selection_reply)))
606140dd 1279 error ("Timed out waiting for reply from selection owner");
8e713be6 1280 if (EQ (XCAR (reading_selection_reply), Qlambda))
d5db4077 1281 error ("No `%s' selection", SDATA (SYMBOL_NAME (selection_symbol)));
ede4db72
RS
1282
1283 /* Otherwise, the selection is waiting for us on the requested property. */
1284 return
5d0ba25b 1285 x_get_window_property_as_lisp_data (display, requestor_window,
ede4db72
RS
1286 target_property, target_type,
1287 selection_atom);
1288}
1289\f
1290/* Subroutines of x_get_window_property_as_lisp_data */
1291
4feb31b2 1292/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1293
ede4db72
RS
1294static void
1295x_get_window_property (display, window, property, data_ret, bytes_ret,
1296 actual_type_ret, actual_format_ret, actual_size_ret,
1297 delete_p)
1298 Display *display;
1299 Window window;
1300 Atom property;
1301 unsigned char **data_ret;
1302 int *bytes_ret;
1303 Atom *actual_type_ret;
1304 int *actual_format_ret;
1305 unsigned long *actual_size_ret;
1306 int delete_p;
1307{
1308 int total_size;
1309 unsigned long bytes_remaining;
1310 int offset = 0;
1311 unsigned char *tmp_data = 0;
1312 int result;
1313 int buffer_size = SELECTION_QUANTUM (display);
1b65481e 1314
d9c0d4a3
GM
1315 if (buffer_size > MAX_SELECTION_QUANTUM)
1316 buffer_size = MAX_SELECTION_QUANTUM;
1b65481e 1317
ede4db72 1318 BLOCK_INPUT;
1b65481e 1319
ede4db72
RS
1320 /* First probe the thing to find out how big it is. */
1321 result = XGetWindowProperty (display, window, property,
137edb72 1322 0L, 0L, False, AnyPropertyType,
ede4db72
RS
1323 actual_type_ret, actual_format_ret,
1324 actual_size_ret,
1325 &bytes_remaining, &tmp_data);
ede4db72
RS
1326 if (result != Success)
1327 {
2f65feb6 1328 UNBLOCK_INPUT;
ede4db72
RS
1329 *data_ret = 0;
1330 *bytes_ret = 0;
1331 return;
1332 }
1b65481e 1333
0158abbc
RS
1334 /* This was allocated by Xlib, so use XFree. */
1335 XFree ((char *) tmp_data);
1b65481e 1336
ede4db72
RS
1337 if (*actual_type_ret == None || *actual_format_ret == 0)
1338 {
2f65feb6 1339 UNBLOCK_INPUT;
ede4db72
RS
1340 return;
1341 }
1342
1343 total_size = bytes_remaining + 1;
1344 *data_ret = (unsigned char *) xmalloc (total_size);
1b65481e 1345
2a1a4c9d 1346 /* Now read, until we've gotten it all. */
ede4db72
RS
1347 while (bytes_remaining)
1348 {
d9c0d4a3 1349#ifdef TRACE_SELECTION
ede4db72
RS
1350 int last = bytes_remaining;
1351#endif
1352 result
1353 = XGetWindowProperty (display, window, property,
137edb72 1354 (long)offset/4, (long)buffer_size/4,
2f65feb6 1355 False,
ede4db72
RS
1356 AnyPropertyType,
1357 actual_type_ret, actual_format_ret,
1358 actual_size_ret, &bytes_remaining, &tmp_data);
d9c0d4a3
GM
1359
1360 TRACE2 ("Read %ld bytes from property %s",
1361 last - bytes_remaining,
1362 XGetAtomName (display, property));
1363
ede4db72
RS
1364 /* If this doesn't return Success at this point, it means that
1365 some clod deleted the selection while we were in the midst of
d9c0d4a3
GM
1366 reading it. Deal with that, I guess.... */
1367 if (result != Success)
1368 break;
ede4db72
RS
1369 *actual_size_ret *= *actual_format_ret / 8;
1370 bcopy (tmp_data, (*data_ret) + offset, *actual_size_ret);
1371 offset += *actual_size_ret;
1b65481e 1372
0158abbc
RS
1373 /* This was allocated by Xlib, so use XFree. */
1374 XFree ((char *) tmp_data);
ede4db72 1375 }
2f65feb6 1376
5c3a351a 1377 XFlush (display);
ede4db72
RS
1378 UNBLOCK_INPUT;
1379 *bytes_ret = offset;
1380}
1381\f
4feb31b2 1382/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1383
ede4db72
RS
1384static void
1385receive_incremental_selection (display, window, property, target_type,
1386 min_size_bytes, data_ret, size_bytes_ret,
1387 type_ret, format_ret, size_ret)
1388 Display *display;
1389 Window window;
1390 Atom property;
1391 Lisp_Object target_type; /* for error messages only */
1392 unsigned int min_size_bytes;
1393 unsigned char **data_ret;
1394 int *size_bytes_ret;
1395 Atom *type_ret;
1396 unsigned long *size_ret;
1397 int *format_ret;
1398{
1399 int offset = 0;
d1f21a66 1400 struct prop_location *wait_object;
ede4db72
RS
1401 *size_bytes_ret = min_size_bytes;
1402 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
d9c0d4a3
GM
1403
1404 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
2f65feb6
RS
1405
1406 /* At this point, we have read an INCR property.
1407 Delete the property to ack it.
1408 (But first, prepare to receive the next event in this handshake.)
ede4db72
RS
1409
1410 Now, we must loop, waiting for the sending window to put a value on
1411 that property, then reading the property, then deleting it to ack.
1412 We are done when the sender places a property of length 0.
1413 */
2f65feb6
RS
1414 BLOCK_INPUT;
1415 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
d9c0d4a3
GM
1416 TRACE1 (" Delete property %s",
1417 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
2f65feb6 1418 XDeleteProperty (display, window, property);
d9c0d4a3
GM
1419 TRACE1 (" Expect new value of property %s",
1420 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
d1f21a66
RS
1421 wait_object = expect_property_change (display, window, property,
1422 PropertyNewValue);
5c3a351a 1423 XFlush (display);
2f65feb6
RS
1424 UNBLOCK_INPUT;
1425
ede4db72
RS
1426 while (1)
1427 {
1428 unsigned char *tmp_data;
1429 int tmp_size_bytes;
d9c0d4a3
GM
1430
1431 TRACE0 (" Wait for property change");
d1f21a66 1432 wait_for_property_change (wait_object);
1b65481e 1433
ede4db72 1434 /* expect it again immediately, because x_get_window_property may
2a1a4c9d 1435 .. no it won't, I don't get it.
d9c0d4a3
GM
1436 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1437 TRACE0 (" Get property value");
ede4db72
RS
1438 x_get_window_property (display, window, property,
1439 &tmp_data, &tmp_size_bytes,
1440 type_ret, format_ret, size_ret, 1);
1441
d9c0d4a3
GM
1442 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1443
ede4db72
RS
1444 if (tmp_size_bytes == 0) /* we're done */
1445 {
d9c0d4a3
GM
1446 TRACE0 ("Done reading incrementally");
1447
2f65feb6
RS
1448 if (! waiting_for_other_props_on_window (display, window))
1449 XSelectInput (display, window, STANDARD_EVENT_SET);
d1f21a66 1450 unexpect_property_change (wait_object);
4feb31b2 1451 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1452 calls xmalloc itself. */
4feb31b2 1453 if (tmp_data) xfree (tmp_data);
ede4db72
RS
1454 break;
1455 }
2f65feb6
RS
1456
1457 BLOCK_INPUT;
d9c0d4a3
GM
1458 TRACE1 (" ACK by deleting property %s",
1459 XGetAtomName (display, property));
2f65feb6 1460 XDeleteProperty (display, window, property);
d1f21a66
RS
1461 wait_object = expect_property_change (display, window, property,
1462 PropertyNewValue);
5c3a351a 1463 XFlush (display);
2f65feb6
RS
1464 UNBLOCK_INPUT;
1465
ede4db72
RS
1466 if (*size_bytes_ret < offset + tmp_size_bytes)
1467 {
ede4db72
RS
1468 *size_bytes_ret = offset + tmp_size_bytes;
1469 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1470 }
1b65481e 1471
018cfa07 1472 bcopy (tmp_data, (*data_ret) + offset, tmp_size_bytes);
ede4db72 1473 offset += tmp_size_bytes;
1b65481e 1474
4feb31b2 1475 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1476 calls xmalloc itself. */
4feb31b2 1477 xfree (tmp_data);
ede4db72
RS
1478 }
1479}
d9c0d4a3 1480
ede4db72
RS
1481\f
1482/* Once a requested selection is "ready" (we got a SelectionNotify event),
1483 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1484 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1485
1486static Lisp_Object
1487x_get_window_property_as_lisp_data (display, window, property, target_type,
1488 selection_atom)
1489 Display *display;
1490 Window window;
1491 Atom property;
1492 Lisp_Object target_type; /* for error messages only */
1493 Atom selection_atom; /* for error messages only */
1494{
1495 Atom actual_type;
1496 int actual_format;
1497 unsigned long actual_size;
1498 unsigned char *data = 0;
1499 int bytes = 0;
1500 Lisp_Object val;
5c3a351a 1501 struct x_display_info *dpyinfo = x_display_info_for_display (display);
ede4db72 1502
d9c0d4a3
GM
1503 TRACE0 ("Reading selection data");
1504
ede4db72
RS
1505 x_get_window_property (display, window, property, &data, &bytes,
1506 &actual_type, &actual_format, &actual_size, 1);
1507 if (! data)
1508 {
1509 int there_is_a_selection_owner;
1510 BLOCK_INPUT;
1511 there_is_a_selection_owner
1512 = XGetSelectionOwner (display, selection_atom);
1513 UNBLOCK_INPUT;
5a499696
RS
1514 Fsignal (Qerror,
1515 there_is_a_selection_owner
1516 ? Fcons (build_string ("selection owner couldn't convert"),
ede4db72
RS
1517 actual_type
1518 ? Fcons (target_type,
d9c0d4a3 1519 Fcons (x_atom_to_symbol (display,
5c3a351a 1520 actual_type),
ede4db72
RS
1521 Qnil))
1522 : Fcons (target_type, Qnil))
5a499696 1523 : Fcons (build_string ("no selection"),
d9c0d4a3 1524 Fcons (x_atom_to_symbol (display,
5a499696
RS
1525 selection_atom),
1526 Qnil)));
ede4db72 1527 }
1b65481e 1528
5c3a351a 1529 if (actual_type == dpyinfo->Xatom_INCR)
ede4db72
RS
1530 {
1531 /* That wasn't really the data, just the beginning. */
1532
1533 unsigned int min_size_bytes = * ((unsigned int *) data);
1534 BLOCK_INPUT;
4feb31b2 1535 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1536 calls xmalloc itself. */
4feb31b2 1537 xfree ((char *) data);
ede4db72
RS
1538 UNBLOCK_INPUT;
1539 receive_incremental_selection (display, window, property, target_type,
1540 min_size_bytes, &data, &bytes,
1541 &actual_type, &actual_format,
1542 &actual_size);
1543 }
1544
2f65feb6 1545 BLOCK_INPUT;
d9c0d4a3 1546 TRACE1 (" Delete property %s", XGetAtomName (display, property));
2f65feb6 1547 XDeleteProperty (display, window, property);
5c3a351a 1548 XFlush (display);
2f65feb6
RS
1549 UNBLOCK_INPUT;
1550
ede4db72
RS
1551 /* It's been read. Now convert it to a lisp object in some semi-rational
1552 manner. */
1553 val = selection_data_to_lisp_data (display, data, bytes,
1554 actual_type, actual_format);
1b65481e 1555
4feb31b2 1556 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1557 calls xmalloc itself. */
4feb31b2 1558 xfree ((char *) data);
ede4db72
RS
1559 return val;
1560}
1561\f
1562/* These functions convert from the selection data read from the server into
1563 something that we can use from Lisp, and vice versa.
1564
1565 Type: Format: Size: Lisp Type:
1566 ----- ------- ----- -----------
1567 * 8 * String
1568 ATOM 32 1 Symbol
1569 ATOM 32 > 1 Vector of Symbols
1570 * 16 1 Integer
1571 * 16 > 1 Vector of Integers
1572 * 32 1 if <=16 bits: Integer
1573 if > 16 bits: Cons of top16, bot16
1574 * 32 > 1 Vector of the above
1575
1576 When converting a Lisp number to C, it is assumed to be of format 16 if
1577 it is an integer, and of format 32 if it is a cons of two integers.
1578
1579 When converting a vector of numbers from Lisp to C, it is assumed to be
1580 of format 16 if every element in the vector is an integer, and is assumed
1581 to be of format 32 if any element is a cons of two integers.
1582
1583 When converting an object to C, it may be of the form (SYMBOL . <data>)
1584 where SYMBOL is what we should claim that the type is. Format and
1585 representation are as above. */
1586
1587
1588
1589static Lisp_Object
1590selection_data_to_lisp_data (display, data, size, type, format)
1591 Display *display;
1592 unsigned char *data;
1593 Atom type;
1594 int size, format;
1595{
5c3a351a 1596 struct x_display_info *dpyinfo = x_display_info_for_display (display);
ede4db72 1597
5c3a351a 1598 if (type == dpyinfo->Xatom_NULL)
ede4db72
RS
1599 return QNULL;
1600
1601 /* Convert any 8-bit data to a string, for compactness. */
1602 else if (format == 8)
e6c7c988
KH
1603 {
1604 Lisp_Object str;
f78da51e
KH
1605 int require_encoding = 0;
1606
d24e26b1
RS
1607 if (
1608#if 1
1609 1
1610#else
1611 ! NILP (buffer_defaults.enable_multibyte_characters)
1612#endif
1613 )
f78da51e 1614 {
5faa9b45
KH
1615 /* If TYPE is `TEXT' or `COMPOUND_TEXT', we should decode
1616 DATA to Emacs internal format because DATA may be encoded
1617 in compound text format. In addtion, if TYPE is `STRING'
1618 and DATA contains any 8-bit Latin-1 code, we should also
1619 decode it. */
1620 if (type == dpyinfo->Xatom_TEXT
1621 || type == dpyinfo->Xatom_COMPOUND_TEXT)
1622 require_encoding = 1;
1623 else if (type == XA_STRING)
f78da51e 1624 {
5faa9b45
KH
1625 int i;
1626 for (i = 0; i < size; i++)
f78da51e 1627 {
5faa9b45
KH
1628 if (data[i] >= 0x80)
1629 {
1630 require_encoding = 1;
1631 break;
1632 }
f78da51e
KH
1633 }
1634 }
1635 }
1636 if (!require_encoding)
061bda8d
KH
1637 {
1638 str = make_unibyte_string ((char *) data, size);
1639 Vlast_coding_system_used = Qraw_text;
1640 }
e6c7c988
KH
1641 else
1642 {
9391d06a 1643 int bufsize;
e6c7c988
KH
1644 unsigned char *buf;
1645 struct coding_system coding;
e6c7c988 1646
16cd5029
KH
1647 if (NILP (Vnext_selection_coding_system))
1648 Vnext_selection_coding_system = Vselection_coding_system;
2584c9ec 1649 setup_coding_system
16cd5029 1650 (Fcheck_coding_system(Vnext_selection_coding_system), &coding);
9846d9db
KH
1651 coding.src_multibyte = 0;
1652 coding.dst_multibyte = 1;
16cd5029 1653 Vnext_selection_coding_system = Qnil;
9391d06a 1654 coding.mode |= CODING_MODE_LAST_BLOCK;
e1885335
KH
1655 /* We explicitely disable composition handling because
1656 selection data should not contain any composition
1657 sequence. */
1658 coding.composing = COMPOSITION_DISABLED;
e6c7c988
KH
1659 bufsize = decoding_buffer_size (&coding, size);
1660 buf = (unsigned char *) xmalloc (bufsize);
9391d06a 1661 decode_coding (&coding, data, buf, size, bufsize);
9846d9db
KH
1662 str = make_string_from_bytes ((char *) buf,
1663 coding.produced_char, coding.produced);
4feb31b2 1664 xfree (buf);
fbbe0ace
EZ
1665
1666 if (SYMBOLP (coding.post_read_conversion)
1667 && !NILP (Ffboundp (coding.post_read_conversion)))
ecb7ac21 1668 str = run_pre_post_conversion_on_str (str, &coding, 0);
061bda8d 1669 Vlast_coding_system_used = coding.symbol;
e6c7c988 1670 }
d5db4077 1671 compose_chars_in_text (0, SCHARS (str), str);
e6c7c988
KH
1672 return str;
1673 }
ede4db72
RS
1674 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1675 a vector of symbols.
1676 */
1677 else if (type == XA_ATOM)
1678 {
1679 int i;
1680 if (size == sizeof (Atom))
d9c0d4a3 1681 return x_atom_to_symbol (display, *((Atom *) data));
ede4db72
RS
1682 else
1683 {
e607a484
RS
1684 Lisp_Object v = Fmake_vector (make_number (size / sizeof (Atom)),
1685 make_number (0));
ede4db72 1686 for (i = 0; i < size / sizeof (Atom); i++)
e607a484 1687 Faset (v, make_number (i),
d9c0d4a3 1688 x_atom_to_symbol (display, ((Atom *) data) [i]));
ede4db72
RS
1689 return v;
1690 }
1691 }
1692
1693 /* Convert a single 16 or small 32 bit number to a Lisp_Int.
1694 If the number is > 16 bits, convert it to a cons of integers,
1695 16 bits in each half.
1696 */
1697 else if (format == 32 && size == sizeof (long))
1698 return long_to_cons (((unsigned long *) data) [0]);
1699 else if (format == 16 && size == sizeof (short))
1700 return make_number ((int) (((unsigned short *) data) [0]));
1701
1702 /* Convert any other kind of data to a vector of numbers, represented
1703 as above (as an integer, or a cons of two 16 bit integers.)
1704 */
1705 else if (format == 16)
1706 {
1707 int i;
937a3875
RS
1708 Lisp_Object v;
1709 v = Fmake_vector (make_number (size / 2), make_number (0));
1710 for (i = 0; i < size / 2; i++)
ede4db72
RS
1711 {
1712 int j = (int) ((unsigned short *) data) [i];
e607a484 1713 Faset (v, make_number (i), make_number (j));
ede4db72
RS
1714 }
1715 return v;
1716 }
1717 else
1718 {
1719 int i;
e607a484 1720 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
ede4db72
RS
1721 for (i = 0; i < size / 4; i++)
1722 {
1723 unsigned long j = ((unsigned long *) data) [i];
e607a484 1724 Faset (v, make_number (i), long_to_cons (j));
ede4db72
RS
1725 }
1726 return v;
1727 }
1728}
1729
1730
4feb31b2 1731/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1732
ede4db72
RS
1733static void
1734lisp_data_to_selection_data (display, obj,
aca39f42
RS
1735 data_ret, type_ret, size_ret,
1736 format_ret, nofree_ret)
ede4db72
RS
1737 Display *display;
1738 Lisp_Object obj;
1739 unsigned char **data_ret;
1740 Atom *type_ret;
1741 unsigned int *size_ret;
1742 int *format_ret;
aca39f42 1743 int *nofree_ret;
ede4db72
RS
1744{
1745 Lisp_Object type = Qnil;
5c3a351a 1746 struct x_display_info *dpyinfo = x_display_info_for_display (display);
aca39f42
RS
1747
1748 *nofree_ret = 0;
1749
8e713be6 1750 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
ede4db72 1751 {
8e713be6
KR
1752 type = XCAR (obj);
1753 obj = XCDR (obj);
1754 if (CONSP (obj) && NILP (XCDR (obj)))
1755 obj = XCAR (obj);
ede4db72
RS
1756 }
1757
1758 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1759 { /* This is not the same as declining */
1760 *format_ret = 32;
1761 *size_ret = 0;
1762 *data_ret = 0;
1763 type = QNULL;
1764 }
1765 else if (STRINGP (obj))
1766 {
5109c8dd 1767 xassert (! STRING_MULTIBYTE (obj));
7b9ae523 1768 if (NILP (type))
5109c8dd
KH
1769 type = QSTRING;
1770 *format_ret = 8;
1771 *size_ret = SBYTES (obj);
1772 *data_ret = SDATA (obj);
1773 *nofree_ret = 1;
ede4db72
RS
1774 }
1775 else if (SYMBOLP (obj))
1776 {
1777 *format_ret = 32;
1778 *size_ret = 1;
1779 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1780 (*data_ret) [sizeof (Atom)] = 0;
5c3a351a 1781 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
ede4db72
RS
1782 if (NILP (type)) type = QATOM;
1783 }
7da64e5c 1784 else if (INTEGERP (obj)
ede4db72
RS
1785 && XINT (obj) < 0xFFFF
1786 && XINT (obj) > -0xFFFF)
1787 {
1788 *format_ret = 16;
1789 *size_ret = 1;
1790 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1791 (*data_ret) [sizeof (short)] = 0;
1792 (*(short **) data_ret) [0] = (short) XINT (obj);
1793 if (NILP (type)) type = QINTEGER;
1794 }
a87ed99c 1795 else if (INTEGERP (obj)
8e713be6
KR
1796 || (CONSP (obj) && INTEGERP (XCAR (obj))
1797 && (INTEGERP (XCDR (obj))
1798 || (CONSP (XCDR (obj))
1799 && INTEGERP (XCAR (XCDR (obj)))))))
ede4db72
RS
1800 {
1801 *format_ret = 32;
1802 *size_ret = 1;
1803 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1804 (*data_ret) [sizeof (long)] = 0;
1805 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1806 if (NILP (type)) type = QINTEGER;
1807 }
1808 else if (VECTORP (obj))
1809 {
1810 /* Lisp_Vectors may represent a set of ATOMs;
1811 a set of 16 or 32 bit INTEGERs;
1812 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1813 */
1814 int i;
1815
1816 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1817 /* This vector is an ATOM set */
1818 {
1819 if (NILP (type)) type = QATOM;
1820 *size_ret = XVECTOR (obj)->size;
1821 *format_ret = 32;
1822 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1823 for (i = 0; i < *size_ret; i++)
1824 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1825 (*(Atom **) data_ret) [i]
5c3a351a 1826 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
ede4db72
RS
1827 else
1828 Fsignal (Qerror, /* Qselection_error */
1829 Fcons (build_string
1830 ("all elements of selection vector must have same type"),
1831 Fcons (obj, Qnil)));
1832 }
1833#if 0 /* #### MULTIPLE doesn't work yet */
1834 else if (VECTORP (XVECTOR (obj)->contents [0]))
1835 /* This vector is an ATOM_PAIR set */
1836 {
1837 if (NILP (type)) type = QATOM_PAIR;
1838 *size_ret = XVECTOR (obj)->size;
1839 *format_ret = 32;
1840 *data_ret = (unsigned char *)
1841 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1842 for (i = 0; i < *size_ret; i++)
1843 if (VECTORP (XVECTOR (obj)->contents [i]))
1844 {
1845 Lisp_Object pair = XVECTOR (obj)->contents [i];
1846 if (XVECTOR (pair)->size != 2)
1847 Fsignal (Qerror,
1b65481e 1848 Fcons (build_string
ede4db72
RS
1849 ("elements of the vector must be vectors of exactly two elements"),
1850 Fcons (pair, Qnil)));
1b65481e 1851
ede4db72 1852 (*(Atom **) data_ret) [i * 2]
5c3a351a
RS
1853 = symbol_to_x_atom (dpyinfo, display,
1854 XVECTOR (pair)->contents [0]);
ede4db72 1855 (*(Atom **) data_ret) [(i * 2) + 1]
5c3a351a
RS
1856 = symbol_to_x_atom (dpyinfo, display,
1857 XVECTOR (pair)->contents [1]);
ede4db72
RS
1858 }
1859 else
1860 Fsignal (Qerror,
1861 Fcons (build_string
1862 ("all elements of the vector must be of the same type"),
1863 Fcons (obj, Qnil)));
1b65481e 1864
ede4db72
RS
1865 }
1866#endif
1867 else
1868 /* This vector is an INTEGER set, or something like it */
1869 {
1870 *size_ret = XVECTOR (obj)->size;
1871 if (NILP (type)) type = QINTEGER;
1872 *format_ret = 16;
1873 for (i = 0; i < *size_ret; i++)
1874 if (CONSP (XVECTOR (obj)->contents [i]))
1875 *format_ret = 32;
7da64e5c 1876 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
ede4db72
RS
1877 Fsignal (Qerror, /* Qselection_error */
1878 Fcons (build_string
1879 ("elements of selection vector must be integers or conses of integers"),
1880 Fcons (obj, Qnil)));
1881
1882 *data_ret = (unsigned char *) xmalloc (*size_ret * (*format_ret/8));
1883 for (i = 0; i < *size_ret; i++)
1884 if (*format_ret == 32)
1885 (*((unsigned long **) data_ret)) [i]
1886 = cons_to_long (XVECTOR (obj)->contents [i]);
1887 else
1888 (*((unsigned short **) data_ret)) [i]
1889 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1890 }
1891 }
1892 else
1893 Fsignal (Qerror, /* Qselection_error */
1894 Fcons (build_string ("unrecognised selection data"),
1895 Fcons (obj, Qnil)));
1896
5c3a351a 1897 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
ede4db72
RS
1898}
1899
1900static Lisp_Object
1901clean_local_selection_data (obj)
1902 Lisp_Object obj;
1903{
1904 if (CONSP (obj)
8e713be6
KR
1905 && INTEGERP (XCAR (obj))
1906 && CONSP (XCDR (obj))
1907 && INTEGERP (XCAR (XCDR (obj)))
1908 && NILP (XCDR (XCDR (obj))))
1909 obj = Fcons (XCAR (obj), XCDR (obj));
ede4db72
RS
1910
1911 if (CONSP (obj)
8e713be6
KR
1912 && INTEGERP (XCAR (obj))
1913 && INTEGERP (XCDR (obj)))
ede4db72 1914 {
8e713be6
KR
1915 if (XINT (XCAR (obj)) == 0)
1916 return XCDR (obj);
1917 if (XINT (XCAR (obj)) == -1)
1918 return make_number (- XINT (XCDR (obj)));
ede4db72
RS
1919 }
1920 if (VECTORP (obj))
1921 {
1922 int i;
1923 int size = XVECTOR (obj)->size;
1924 Lisp_Object copy;
1925 if (size == 1)
1926 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
e607a484 1927 copy = Fmake_vector (make_number (size), Qnil);
ede4db72
RS
1928 for (i = 0; i < size; i++)
1929 XVECTOR (copy)->contents [i]
1930 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1931 return copy;
1932 }
1933 return obj;
1934}
1935\f
1936/* Called from XTread_socket to handle SelectionNotify events.
606140dd
KH
1937 If it's the selection we are waiting for, stop waiting
1938 by setting the car of reading_selection_reply to non-nil.
1939 We store t there if the reply is successful, lambda if not. */
ede4db72
RS
1940
1941void
1942x_handle_selection_notify (event)
1943 XSelectionEvent *event;
1944{
5d0ba25b 1945 if (event->requestor != reading_selection_window)
ede4db72
RS
1946 return;
1947 if (event->selection != reading_which_selection)
1948 return;
1949
d9c0d4a3 1950 TRACE0 ("Received SelectionNotify");
f3fbd155
KR
1951 XSETCAR (reading_selection_reply,
1952 (event->property != 0 ? Qt : Qlambda));
ede4db72
RS
1953}
1954
1955\f
a0d76c27 1956DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
8c1a1077
PJ
1957 Sx_own_selection_internal, 2, 2, 0,
1958 doc: /* Assert an X selection of the given TYPE with the given VALUE.
1959TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1960\(Those are literal upper-case symbol names, since that's what X expects.)
1961VALUE is typically a string, or a cons of two markers, but may be
1962anything that the functions on `selection-converter-alist' know about. */)
1963 (selection_name, selection_value)
ede4db72
RS
1964 Lisp_Object selection_name, selection_value;
1965{
703e0710 1966 check_x ();
b7826503 1967 CHECK_SYMBOL (selection_name);
606140dd 1968 if (NILP (selection_value)) error ("selection-value may not be nil");
ede4db72
RS
1969 x_own_selection (selection_name, selection_value);
1970 return selection_value;
1971}
1972
1973
1974/* Request the selection value from the owner. If we are the owner,
1975 simply return our selection value. If we are not the owner, this
1976 will block until all of the data has arrived. */
1977
a0d76c27 1978DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
8c1a1077
PJ
1979 Sx_get_selection_internal, 2, 2, 0,
1980 doc: /* Return text selected from some X window.
1981SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1982\(Those are literal upper-case symbol names, since that's what X expects.)
1983TYPE is the type of data desired, typically `STRING'. */)
1984 (selection_symbol, target_type)
ede4db72
RS
1985 Lisp_Object selection_symbol, target_type;
1986{
1987 Lisp_Object val = Qnil;
1988 struct gcpro gcpro1, gcpro2;
1989 GCPRO2 (target_type, val); /* we store newly consed data into these */
703e0710 1990 check_x ();
b7826503 1991 CHECK_SYMBOL (selection_symbol);
ede4db72
RS
1992
1993#if 0 /* #### MULTIPLE doesn't work yet */
1994 if (CONSP (target_type)
8e713be6 1995 && XCAR (target_type) == QMULTIPLE)
ede4db72 1996 {
b7826503 1997 CHECK_VECTOR (XCDR (target_type));
ede4db72
RS
1998 /* So we don't destructively modify this... */
1999 target_type = copy_multiple_data (target_type);
2000 }
2001 else
2002#endif
b7826503 2003 CHECK_SYMBOL (target_type);
ede4db72 2004
5109c8dd 2005 val = x_get_local_selection (selection_symbol, target_type, 1);
ede4db72
RS
2006
2007 if (NILP (val))
2008 {
2009 val = x_get_foreign_selection (selection_symbol, target_type);
2010 goto DONE;
2011 }
2012
2013 if (CONSP (val)
8e713be6 2014 && SYMBOLP (XCAR (val)))
ede4db72 2015 {
8e713be6
KR
2016 val = XCDR (val);
2017 if (CONSP (val) && NILP (XCDR (val)))
2018 val = XCAR (val);
ede4db72
RS
2019 }
2020 val = clean_local_selection_data (val);
2021 DONE:
2022 UNGCPRO;
2023 return val;
2024}
2025
a0d76c27 2026DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
8c1a1077
PJ
2027 Sx_disown_selection_internal, 1, 2, 0,
2028 doc: /* If we own the selection SELECTION, disown it.
2029Disowning it means there is no such selection. */)
2030 (selection, time)
ede4db72
RS
2031 Lisp_Object selection;
2032 Lisp_Object time;
2033{
ede4db72
RS
2034 Time timestamp;
2035 Atom selection_atom;
44d46531 2036 struct selection_input_event event;
3834c318 2037 Display *display;
5c3a351a 2038 struct x_display_info *dpyinfo;
378c33ca 2039 struct frame *sf = SELECTED_FRAME ();
ede4db72 2040
703e0710 2041 check_x ();
378c33ca
GM
2042 display = FRAME_X_DISPLAY (sf);
2043 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
b7826503 2044 CHECK_SYMBOL (selection);
ede4db72 2045 if (NILP (time))
7da64e5c 2046 timestamp = last_event_timestamp;
ede4db72
RS
2047 else
2048 timestamp = cons_to_long (time);
2049
2050 if (NILP (assq_no_quit (selection, Vselection_alist)))
2051 return Qnil; /* Don't disown the selection when we're not the owner. */
2052
5c3a351a 2053 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
ede4db72
RS
2054
2055 BLOCK_INPUT;
2056 XSetSelectionOwner (display, selection_atom, None, timestamp);
2057 UNBLOCK_INPUT;
2058
eb8c3be9 2059 /* It doesn't seem to be guaranteed that a SelectionClear event will be
ede4db72
RS
2060 generated for a window which owns the selection when that window sets
2061 the selection owner to None. The NCD server does, the MIT Sun4 server
2062 doesn't. So we synthesize one; this means we might get two, but
2063 that's ok, because the second one won't have any effect. */
8d47f8c4
RS
2064 SELECTION_EVENT_DISPLAY (&event) = display;
2065 SELECTION_EVENT_SELECTION (&event) = selection_atom;
2066 SELECTION_EVENT_TIME (&event) = timestamp;
44d46531 2067 x_handle_selection_clear ((struct input_event *) &event);
ede4db72
RS
2068
2069 return Qt;
2070}
2071
a87ed99c
RS
2072/* Get rid of all the selections in buffer BUFFER.
2073 This is used when we kill a buffer. */
2074
2075void
2076x_disown_buffer_selections (buffer)
2077 Lisp_Object buffer;
2078{
2079 Lisp_Object tail;
2080 struct buffer *buf = XBUFFER (buffer);
2081
8e713be6 2082 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
a87ed99c
RS
2083 {
2084 Lisp_Object elt, value;
8e713be6
KR
2085 elt = XCAR (tail);
2086 value = XCDR (elt);
2087 if (CONSP (value) && MARKERP (XCAR (value))
2088 && XMARKER (XCAR (value))->buffer == buf)
2089 Fx_disown_selection_internal (XCAR (elt), Qnil);
a87ed99c
RS
2090 }
2091}
ede4db72
RS
2092
2093DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
8c1a1077
PJ
2094 0, 1, 0,
2095 doc: /* Whether the current Emacs process owns the given X Selection.
2096The arg should be the name of the selection in question, typically one of
2097the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2098\(Those are literal upper-case symbol names, since that's what X expects.)
2099For convenience, the symbol nil is the same as `PRIMARY',
2100and t is the same as `SECONDARY'. */)
2101 (selection)
ede4db72
RS
2102 Lisp_Object selection;
2103{
703e0710 2104 check_x ();
b7826503 2105 CHECK_SYMBOL (selection);
ede4db72
RS
2106 if (EQ (selection, Qnil)) selection = QPRIMARY;
2107 if (EQ (selection, Qt)) selection = QSECONDARY;
1b65481e 2108
ede4db72
RS
2109 if (NILP (Fassq (selection, Vselection_alist)))
2110 return Qnil;
2111 return Qt;
2112}
2113
2114DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
8c1a1077
PJ
2115 0, 1, 0,
2116 doc: /* Whether there is an owner for the given X Selection.
2117The arg should be the name of the selection in question, typically one of
2118the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2119\(Those are literal upper-case symbol names, since that's what X expects.)
2120For convenience, the symbol nil is the same as `PRIMARY',
2121and t is the same as `SECONDARY'. */)
2122 (selection)
ede4db72
RS
2123 Lisp_Object selection;
2124{
2125 Window owner;
356ba514 2126 Atom atom;
3834c318 2127 Display *dpy;
378c33ca 2128 struct frame *sf = SELECTED_FRAME ();
3834c318 2129
b8c70430 2130 /* It should be safe to call this before we have an X frame. */
378c33ca 2131 if (! FRAME_X_P (sf))
b8c70430
RS
2132 return Qnil;
2133
378c33ca 2134 dpy = FRAME_X_DISPLAY (sf);
b7826503 2135 CHECK_SYMBOL (selection);
ede4db72
RS
2136 if (!NILP (Fx_selection_owner_p (selection)))
2137 return Qt;
356ba514
RS
2138 if (EQ (selection, Qnil)) selection = QPRIMARY;
2139 if (EQ (selection, Qt)) selection = QSECONDARY;
378c33ca 2140 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
356ba514
RS
2141 if (atom == 0)
2142 return Qnil;
ede4db72 2143 BLOCK_INPUT;
356ba514 2144 owner = XGetSelectionOwner (dpy, atom);
ede4db72
RS
2145 UNBLOCK_INPUT;
2146 return (owner ? Qt : Qnil);
2147}
2148
2149\f
2150#ifdef CUT_BUFFER_SUPPORT
2151
ede4db72
RS
2152/* Ensure that all 8 cut buffers exist. ICCCM says we gotta... */
2153static void
2154initialize_cut_buffers (display, window)
2155 Display *display;
2156 Window window;
2157{
2158 unsigned char *data = (unsigned char *) "";
2159 BLOCK_INPUT;
2160#define FROB(atom) XChangeProperty (display, window, atom, XA_STRING, 8, \
2161 PropModeAppend, data, 0)
2162 FROB (XA_CUT_BUFFER0);
2163 FROB (XA_CUT_BUFFER1);
2164 FROB (XA_CUT_BUFFER2);
2165 FROB (XA_CUT_BUFFER3);
2166 FROB (XA_CUT_BUFFER4);
2167 FROB (XA_CUT_BUFFER5);
2168 FROB (XA_CUT_BUFFER6);
2169 FROB (XA_CUT_BUFFER7);
2170#undef FROB
2171 UNBLOCK_INPUT;
ede4db72
RS
2172}
2173
2174
b7826503
PJ
2175#define CHECK_CUT_BUFFER(symbol) \
2176 { CHECK_SYMBOL ((symbol)); \
ede4db72
RS
2177 if (!EQ((symbol), QCUT_BUFFER0) && !EQ((symbol), QCUT_BUFFER1) \
2178 && !EQ((symbol), QCUT_BUFFER2) && !EQ((symbol), QCUT_BUFFER3) \
2179 && !EQ((symbol), QCUT_BUFFER4) && !EQ((symbol), QCUT_BUFFER5) \
2180 && !EQ((symbol), QCUT_BUFFER6) && !EQ((symbol), QCUT_BUFFER7)) \
2181 Fsignal (Qerror, \
a87ed99c 2182 Fcons (build_string ("doesn't name a cut buffer"), \
ede4db72
RS
2183 Fcons ((symbol), Qnil))); \
2184 }
2185
a87ed99c 2186DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
8c1a1077
PJ
2187 Sx_get_cut_buffer_internal, 1, 1, 0,
2188 doc: /* Returns the value of the named cut buffer (typically CUT_BUFFER0). */)
2189 (buffer)
ede4db72
RS
2190 Lisp_Object buffer;
2191{
3834c318 2192 Window window;
ede4db72
RS
2193 Atom buffer_atom;
2194 unsigned char *data;
2195 int bytes;
2196 Atom type;
2197 int format;
2198 unsigned long size;
2199 Lisp_Object ret;
3834c318 2200 Display *display;
5c3a351a 2201 struct x_display_info *dpyinfo;
378c33ca 2202 struct frame *sf = SELECTED_FRAME ();
ede4db72 2203
703e0710 2204 check_x ();
378c33ca
GM
2205 display = FRAME_X_DISPLAY (sf);
2206 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
3834c318 2207 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
b7826503 2208 CHECK_CUT_BUFFER (buffer);
5c3a351a 2209 buffer_atom = symbol_to_x_atom (dpyinfo, display, buffer);
ede4db72
RS
2210
2211 x_get_window_property (display, window, buffer_atom, &data, &bytes,
2212 &type, &format, &size, 0);
22e00739
RS
2213 if (!data || !format)
2214 return Qnil;
1b65481e 2215
ede4db72
RS
2216 if (format != 8 || type != XA_STRING)
2217 Fsignal (Qerror,
2218 Fcons (build_string ("cut buffer doesn't contain 8-bit data"),
d9c0d4a3 2219 Fcons (x_atom_to_symbol (display, type),
ede4db72
RS
2220 Fcons (make_number (format), Qnil))));
2221
2222 ret = (bytes ? make_string ((char *) data, bytes) : Qnil);
4feb31b2 2223 /* Use xfree, not XFree, because x_get_window_property
0158abbc 2224 calls xmalloc itself. */
4feb31b2 2225 xfree (data);
ede4db72
RS
2226 return ret;
2227}
2228
2229
a87ed99c 2230DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
8c1a1077
PJ
2231 Sx_store_cut_buffer_internal, 2, 2, 0,
2232 doc: /* Sets the value of the named cut buffer (typically CUT_BUFFER0). */)
2233 (buffer, string)
ede4db72
RS
2234 Lisp_Object buffer, string;
2235{
3834c318 2236 Window window;
ede4db72
RS
2237 Atom buffer_atom;
2238 unsigned char *data;
2239 int bytes;
2240 int bytes_remaining;
3834c318
RS
2241 int max_bytes;
2242 Display *display;
378c33ca 2243 struct frame *sf = SELECTED_FRAME ();
ede4db72 2244
703e0710 2245 check_x ();
378c33ca 2246 display = FRAME_X_DISPLAY (sf);
3834c318
RS
2247 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2248
2249 max_bytes = SELECTION_QUANTUM (display);
2250 if (max_bytes > MAX_SELECTION_QUANTUM)
2251 max_bytes = MAX_SELECTION_QUANTUM;
2252
b7826503
PJ
2253 CHECK_CUT_BUFFER (buffer);
2254 CHECK_STRING (string);
378c33ca 2255 buffer_atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf),
5c3a351a 2256 display, buffer);
d5db4077
KR
2257 data = (unsigned char *) SDATA (string);
2258 bytes = SBYTES (string);
ede4db72
RS
2259 bytes_remaining = bytes;
2260
378c33ca 2261 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
04649dbc
KH
2262 {
2263 initialize_cut_buffers (display, window);
378c33ca 2264 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
04649dbc 2265 }
ede4db72
RS
2266
2267 BLOCK_INPUT;
10608c8c
RS
2268
2269 /* Don't mess up with an empty value. */
2270 if (!bytes_remaining)
2271 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2272 PropModeReplace, data, 0);
2273
ede4db72
RS
2274 while (bytes_remaining)
2275 {
2276 int chunk = (bytes_remaining < max_bytes
2277 ? bytes_remaining : max_bytes);
2278 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2279 (bytes_remaining == bytes
2280 ? PropModeReplace
2281 : PropModeAppend),
2282 data, chunk);
2283 data += chunk;
2284 bytes_remaining -= chunk;
2285 }
2286 UNBLOCK_INPUT;
2287 return string;
2288}
2289
2290
a87ed99c 2291DEFUN ("x-rotate-cut-buffers-internal", Fx_rotate_cut_buffers_internal,
8c1a1077
PJ
2292 Sx_rotate_cut_buffers_internal, 1, 1, 0,
2293 doc: /* Rotate the values of the cut buffers by the given number of step.
2294Positive means shift the values forward, negative means backward. */)
2295 (n)
ede4db72
RS
2296 Lisp_Object n;
2297{
3834c318
RS
2298 Window window;
2299 Atom props[8];
2300 Display *display;
378c33ca 2301 struct frame *sf = SELECTED_FRAME ();
ede4db72 2302
703e0710 2303 check_x ();
378c33ca 2304 display = FRAME_X_DISPLAY (sf);
3834c318 2305 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
b7826503 2306 CHECK_NUMBER (n);
3834c318
RS
2307 if (XINT (n) == 0)
2308 return n;
378c33ca 2309 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
04649dbc
KH
2310 {
2311 initialize_cut_buffers (display, window);
378c33ca 2312 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
04649dbc 2313 }
3834c318 2314
ede4db72
RS
2315 props[0] = XA_CUT_BUFFER0;
2316 props[1] = XA_CUT_BUFFER1;
2317 props[2] = XA_CUT_BUFFER2;
2318 props[3] = XA_CUT_BUFFER3;
2319 props[4] = XA_CUT_BUFFER4;
2320 props[5] = XA_CUT_BUFFER5;
2321 props[6] = XA_CUT_BUFFER6;
2322 props[7] = XA_CUT_BUFFER7;
2323 BLOCK_INPUT;
2324 XRotateWindowProperties (display, window, props, 8, XINT (n));
2325 UNBLOCK_INPUT;
2326 return n;
2327}
2328
2329#endif
2330\f
ede4db72
RS
2331void
2332syms_of_xselect ()
2333{
ede4db72
RS
2334 defsubr (&Sx_get_selection_internal);
2335 defsubr (&Sx_own_selection_internal);
2336 defsubr (&Sx_disown_selection_internal);
2337 defsubr (&Sx_selection_owner_p);
2338 defsubr (&Sx_selection_exists_p);
2339
2340#ifdef CUT_BUFFER_SUPPORT
a87ed99c
RS
2341 defsubr (&Sx_get_cut_buffer_internal);
2342 defsubr (&Sx_store_cut_buffer_internal);
2343 defsubr (&Sx_rotate_cut_buffers_internal);
ede4db72
RS
2344#endif
2345
2346 reading_selection_reply = Fcons (Qnil, Qnil);
2347 staticpro (&reading_selection_reply);
2348 reading_selection_window = 0;
2349 reading_which_selection = 0;
2350
2351 property_change_wait_list = 0;
2f65feb6 2352 prop_location_identifier = 0;
ede4db72
RS
2353 property_change_reply = Fcons (Qnil, Qnil);
2354 staticpro (&property_change_reply);
2355
2356 Vselection_alist = Qnil;
2357 staticpro (&Vselection_alist);
2358
2359 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
8c1a1077
PJ
2360 doc: /* An alist associating X Windows selection-types with functions.
2361These functions are called to convert the selection, with three args:
2362the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2363a desired type to which the selection should be converted;
2364and the local selection value (whatever was given to `x-own-selection').
2365
2366The function should return the value to send to the X server
2367\(typically a string). A return value of nil
2368means that the conversion could not be done.
2369A return value which is the symbol `NULL'
2370means that a side-effect was executed,
2371and there is no meaningful selection value. */);
ede4db72
RS
2372 Vselection_converter_alist = Qnil;
2373
2374 DEFVAR_LISP ("x-lost-selection-hooks", &Vx_lost_selection_hooks,
8c1a1077
PJ
2375 doc: /* A list of functions to be called when Emacs loses an X selection.
2376\(This happens when some other X client makes its own selection
2377or when a Lisp program explicitly clears the selection.)
2378The functions are called with one argument, the selection type
2379\(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
ede4db72
RS
2380 Vx_lost_selection_hooks = Qnil;
2381
2382 DEFVAR_LISP ("x-sent-selection-hooks", &Vx_sent_selection_hooks,
8c1a1077
PJ
2383 doc: /* A list of functions to be called when Emacs answers a selection request.
2384The functions are called with four arguments:
2385 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2386 - the selection-type which Emacs was asked to convert the
2387 selection into before sending (for example, `STRING' or `LENGTH');
2388 - a flag indicating success or failure for responding to the request.
2389We might have failed (and declined the request) for any number of reasons,
2390including being asked for a selection that we no longer own, or being asked
2391to convert into a type that we don't know about or that is inappropriate.
2392This hook doesn't let you change the behavior of Emacs's selection replies,
2393it merely informs you that they have happened. */);
ede4db72
RS
2394 Vx_sent_selection_hooks = Qnil;
2395
93e4ce1b 2396 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
8c1a1077
PJ
2397 doc: /* Coding system for communicating with other X clients.
2398When sending or receiving text via cut_buffer, selection, and clipboard,
2399the text is encoded or decoded by this coding system.
5a79ea57
EZ
2400The default value is `compound-text-with-extensions'. */);
2401 Vselection_coding_system = intern ("compound-text-with-extensions");
2584c9ec 2402
16cd5029 2403 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
8c1a1077
PJ
2404 doc: /* Coding system for the next communication with other X clients.
2405Usually, `selection-coding-system' is used for communicating with
f8f8d464
JB
2406other X clients. But, if this variable is set, it is used for the
2407next communication only. After the communication, this variable is
8c1a1077 2408set to nil. */);
16cd5029
KH
2409 Vnext_selection_coding_system = Qnil;
2410
ede4db72 2411 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
8c1a1077
PJ
2412 doc: /* Number of milliseconds to wait for a selection reply.
2413If the selection owner doesn't reply in this time, we give up.
2414A value of 0 means wait as long as necessary. This is initialized from the
2415\"*selectionTimeout\" resource. */);
ede4db72
RS
2416 x_selection_timeout = 0;
2417
2418 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
2419 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
2420 QSTRING = intern ("STRING"); staticpro (&QSTRING);
2421 QINTEGER = intern ("INTEGER"); staticpro (&QINTEGER);
2422 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
2423 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2424 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
e6c7c988 2425 QCOMPOUND_TEXT = intern ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
5109c8dd 2426 QUTF8_STRING = intern ("UTF8_STRING"); staticpro (&QUTF8_STRING);
ede4db72
RS
2427 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2428 QDELETE = intern ("DELETE"); staticpro (&QDELETE);
2429 QMULTIPLE = intern ("MULTIPLE"); staticpro (&QMULTIPLE);
2430 QINCR = intern ("INCR"); staticpro (&QINCR);
2431 QEMACS_TMP = intern ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
2432 QTARGETS = intern ("TARGETS"); staticpro (&QTARGETS);
2433 QATOM = intern ("ATOM"); staticpro (&QATOM);
2434 QATOM_PAIR = intern ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
2435 QNULL = intern ("NULL"); staticpro (&QNULL);
5a79ea57
EZ
2436 Qcompound_text_with_extensions = intern ("compound-text-with-extensions");
2437 staticpro (&Qcompound_text_with_extensions);
ede4db72
RS
2438
2439#ifdef CUT_BUFFER_SUPPORT
2440 QCUT_BUFFER0 = intern ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
2441 QCUT_BUFFER1 = intern ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
2442 QCUT_BUFFER2 = intern ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
2443 QCUT_BUFFER3 = intern ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
2444 QCUT_BUFFER4 = intern ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
2445 QCUT_BUFFER5 = intern ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
2446 QCUT_BUFFER6 = intern ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
2447 QCUT_BUFFER7 = intern ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
2448#endif
2449
2450}