Comment change.
[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
ceabd272 732 /* rms, 2003-01-03: I think I have fixed this bug. */
47a6ac17
GM
733 /* The window we're communicating with may have been deleted
734 in the meantime (that's a real situation from a bug report).
735 In this case, there may be events in the event queue still
736 refering to the deleted window, and we'll get a BadWindow error
737 in XTread_socket when processing the events. I don't have
738 an idea how to fix that. gerd, 2001-01-98. */
afe1529d 739 XFlush (display);
5a499696 740 x_uncatch_errors (display, count);
afe1529d 741 UNBLOCK_INPUT;
ede4db72
RS
742}
743\f
744/* Handle a SelectionRequest event EVENT.
745 This is called from keyboard.c when such an event is found in the queue. */
746
747void
748x_handle_selection_request (event)
749 struct input_event *event;
750{
751 struct gcpro gcpro1, gcpro2, gcpro3;
9d2d1dd8 752 Lisp_Object local_selection_data;
ede4db72 753 Lisp_Object selection_symbol;
9d2d1dd8
KH
754 Lisp_Object target_symbol;
755 Lisp_Object converted_selection;
ede4db72 756 Time local_selection_time;
9d2d1dd8 757 Lisp_Object successful_p;
ede4db72 758 int count;
5c3a351a
RS
759 struct x_display_info *dpyinfo
760 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
ede4db72 761
9d2d1dd8
KH
762 local_selection_data = Qnil;
763 target_symbol = Qnil;
764 converted_selection = Qnil;
765 successful_p = Qnil;
766
ede4db72
RS
767 GCPRO3 (local_selection_data, converted_selection, target_symbol);
768
d9c0d4a3 769 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
ede4db72
RS
770 SELECTION_EVENT_SELECTION (event));
771
772 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
ede4db72
RS
773
774 if (NILP (local_selection_data))
775 {
776 /* Someone asked for the selection, but we don't have it any more.
777 */
778 x_decline_selection_request (event);
779 goto DONE;
780 }
781
782 local_selection_time = (Time)
8e713be6 783 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
ede4db72
RS
784
785 if (SELECTION_EVENT_TIME (event) != CurrentTime
7da64e5c 786 && local_selection_time > SELECTION_EVENT_TIME (event))
ede4db72
RS
787 {
788 /* Someone asked for the selection, and we have one, but not the one
789 they're looking for.
790 */
791 x_decline_selection_request (event);
792 goto DONE;
793 }
794
ede4db72 795 x_selection_current_request = event;
331379bf 796 count = SPECPDL_INDEX ();
ca29f2b8 797 selection_request_dpyinfo = dpyinfo;
ede4db72
RS
798 record_unwind_protect (x_selection_request_lisp_error, Qnil);
799
d9c0d4a3 800 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
ede4db72
RS
801 SELECTION_EVENT_TARGET (event));
802
803#if 0 /* #### MULTIPLE doesn't work yet */
804 if (EQ (target_symbol, QMULTIPLE))
805 target_symbol = fetch_multiple_target (event);
806#endif
1b65481e 807
ede4db72 808 /* Convert lisp objects back into binary data */
1b65481e 809
ede4db72 810 converted_selection
5109c8dd 811 = x_get_local_selection (selection_symbol, target_symbol, 0);
1b65481e 812
ede4db72
RS
813 if (! NILP (converted_selection))
814 {
815 unsigned char *data;
816 unsigned int size;
817 int format;
818 Atom type;
aca39f42
RS
819 int nofree;
820
2f65feb6
RS
821 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
822 converted_selection,
aca39f42 823 &data, &type, &size, &format, &nofree);
1b65481e 824
ede4db72
RS
825 x_reply_selection_request (event, format, data, size, type);
826 successful_p = Qt;
827
828 /* Indicate we have successfully processed this event. */
7da64e5c 829 x_selection_current_request = 0;
ede4db72 830
4feb31b2 831 /* Use xfree, not XFree, because lisp_data_to_selection_data
0158abbc 832 calls xmalloc itself. */
aca39f42 833 if (!nofree)
4feb31b2 834 xfree (data);
ede4db72
RS
835 }
836 unbind_to (count, Qnil);
837
838 DONE:
839
840 UNGCPRO;
841
842 /* Let random lisp code notice that the selection has been asked for. */
843 {
9d2d1dd8
KH
844 Lisp_Object rest;
845 rest = Vx_sent_selection_hooks;
ede4db72
RS
846 if (!EQ (rest, Qunbound))
847 for (; CONSP (rest); rest = Fcdr (rest))
848 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
849 }
850}
851\f
e18e6130 852/* Handle a SelectionClear event EVENT, which indicates that some
ede4db72
RS
853 client cleared out our previously asserted selection.
854 This is called from keyboard.c when such an event is found in the queue. */
855
856void
857x_handle_selection_clear (event)
858 struct input_event *event;
859{
860 Display *display = SELECTION_EVENT_DISPLAY (event);
861 Atom selection = SELECTION_EVENT_SELECTION (event);
862 Time changed_owner_time = SELECTION_EVENT_TIME (event);
1b65481e 863
ede4db72
RS
864 Lisp_Object selection_symbol, local_selection_data;
865 Time local_selection_time;
5c3a351a 866 struct x_display_info *dpyinfo = x_display_info_for_display (display);
e18e6130
RS
867 struct x_display_info *t_dpyinfo;
868
869 /* If the new selection owner is also Emacs,
870 don't clear the new selection. */
871 BLOCK_INPUT;
872 /* Check each display on the same terminal,
873 to see if this Emacs job now owns the selection
874 through that display. */
875 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
876 if (t_dpyinfo->kboard == dpyinfo->kboard)
877 {
878 Window owner_window
879 = XGetSelectionOwner (t_dpyinfo->display, selection);
880 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
881 {
882 UNBLOCK_INPUT;
883 return;
884 }
885 }
886 UNBLOCK_INPUT;
ede4db72 887
d9c0d4a3 888 selection_symbol = x_atom_to_symbol (display, selection);
ede4db72
RS
889
890 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
891
892 /* Well, we already believe that we don't own it, so that's just fine. */
893 if (NILP (local_selection_data)) return;
894
895 local_selection_time = (Time)
8e713be6 896 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
ede4db72
RS
897
898 /* This SelectionClear is for a selection that we no longer own, so we can
899 disregard it. (That is, we have reasserted the selection since this
900 request was generated.) */
901
902 if (changed_owner_time != CurrentTime
903 && local_selection_time > changed_owner_time)
904 return;
905
906 /* Otherwise, we're really honest and truly being told to drop it.
907 Don't use Fdelq as that may QUIT;. */
908
909 if (EQ (local_selection_data, Fcar (Vselection_alist)))
910 Vselection_alist = Fcdr (Vselection_alist);
911 else
912 {
913 Lisp_Object rest;
914 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
8e713be6 915 if (EQ (local_selection_data, Fcar (XCDR (rest))))
ede4db72 916 {
f3fbd155 917 XSETCDR (rest, Fcdr (XCDR (rest)));
ede4db72
RS
918 break;
919 }
920 }
921
922 /* Let random lisp code notice that the selection has been stolen. */
923
924 {
d1f21a66
RS
925 Lisp_Object rest;
926 rest = Vx_lost_selection_hooks;
ede4db72 927 if (!EQ (rest, Qunbound))
d1f21a66
RS
928 {
929 for (; CONSP (rest); rest = Fcdr (rest))
930 call1 (Fcar (rest), selection_symbol);
7c6b2ea4 931 prepare_menu_bars ();
3007ebfb 932 redisplay_preserve_echo_area (20);
d1f21a66 933 }
ede4db72
RS
934 }
935}
936
118bd841
RS
937/* Clear all selections that were made from frame F.
938 We do this when about to delete a frame. */
939
940void
941x_clear_frame_selections (f)
942 FRAME_PTR f;
943{
944 Lisp_Object frame;
945 Lisp_Object rest;
946
90851bbe 947 XSETFRAME (frame, f);
118bd841
RS
948
949 /* Otherwise, we're really honest and truly being told to drop it.
950 Don't use Fdelq as that may QUIT;. */
951
0d199f9c
RS
952 /* Delete elements from the beginning of Vselection_alist. */
953 while (!NILP (Vselection_alist)
954 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
955 {
956 /* Let random Lisp code notice that the selection has been stolen. */
957 Lisp_Object hooks, selection_symbol;
958
959 hooks = Vx_lost_selection_hooks;
960 selection_symbol = Fcar (Fcar (Vselection_alist));
961
962 if (!EQ (hooks, Qunbound))
963 {
964 for (; CONSP (hooks); hooks = Fcdr (hooks))
965 call1 (Fcar (hooks), selection_symbol);
996b804d
MB
966#if 0 /* This can crash when deleting a frame
967 from x_connection_closed. Anyway, it seems unnecessary;
968 something else should cause a redisplay. */
3007ebfb 969 redisplay_preserve_echo_area (21);
996b804d 970#endif
0d199f9c
RS
971 }
972
973 Vselection_alist = Fcdr (Vselection_alist);
974 }
975
976 /* Delete elements after the beginning of Vselection_alist. */
118bd841 977 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
8e713be6 978 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
118bd841
RS
979 {
980 /* Let random Lisp code notice that the selection has been stolen. */
981 Lisp_Object hooks, selection_symbol;
982
983 hooks = Vx_lost_selection_hooks;
8e713be6 984 selection_symbol = Fcar (Fcar (XCDR (rest)));
118bd841
RS
985
986 if (!EQ (hooks, Qunbound))
987 {
988 for (; CONSP (hooks); hooks = Fcdr (hooks))
989 call1 (Fcar (hooks), selection_symbol);
996b804d 990#if 0 /* See above */
3007ebfb 991 redisplay_preserve_echo_area (22);
996b804d 992#endif
118bd841 993 }
f3fbd155 994 XSETCDR (rest, Fcdr (XCDR (rest)));
118bd841
RS
995 break;
996 }
997}
ede4db72 998\f
ede4db72
RS
999/* Nonzero if any properties for DISPLAY and WINDOW
1000 are on the list of what we are waiting for. */
1001
1002static int
1003waiting_for_other_props_on_window (display, window)
1004 Display *display;
1005 Window window;
1006{
1007 struct prop_location *rest = property_change_wait_list;
1008 while (rest)
1009 if (rest->display == display && rest->window == window)
1010 return 1;
1011 else
1012 rest = rest->next;
1013 return 0;
1014}
1015
1016/* Add an entry to the list of property changes we are waiting for.
1017 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1018 The return value is a number that uniquely identifies
1019 this awaited property change. */
1020
d1f21a66 1021static struct prop_location *
ede4db72
RS
1022expect_property_change (display, window, property, state)
1023 Display *display;
1024 Window window;
6c070502 1025 Atom property;
ede4db72
RS
1026 int state;
1027{
d9c0d4a3 1028 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
2f65feb6 1029 pl->identifier = ++prop_location_identifier;
ede4db72
RS
1030 pl->display = display;
1031 pl->window = window;
1032 pl->property = property;
1033 pl->desired_state = state;
1034 pl->next = property_change_wait_list;
d1f21a66 1035 pl->arrived = 0;
ede4db72 1036 property_change_wait_list = pl;
d1f21a66 1037 return pl;
ede4db72
RS
1038}
1039
1040/* Delete an entry from the list of property changes we are waiting for.
2f65feb6 1041 IDENTIFIER is the number that uniquely identifies the entry. */
ede4db72
RS
1042
1043static void
d1f21a66
RS
1044unexpect_property_change (location)
1045 struct prop_location *location;
ede4db72
RS
1046{
1047 struct prop_location *prev = 0, *rest = property_change_wait_list;
1048 while (rest)
1049 {
d1f21a66 1050 if (rest == location)
ede4db72
RS
1051 {
1052 if (prev)
1053 prev->next = rest->next;
1054 else
1055 property_change_wait_list = rest->next;
4feb31b2 1056 xfree (rest);
ede4db72
RS
1057 return;
1058 }
1059 prev = rest;
1060 rest = rest->next;
1061 }
1062}
1063
2f65feb6
RS
1064/* Remove the property change expectation element for IDENTIFIER. */
1065
1066static Lisp_Object
1067wait_for_property_change_unwind (identifierval)
1068 Lisp_Object identifierval;
1069{
c4898489 1070 unexpect_property_change ((struct prop_location *)
8e713be6
KR
1071 (XFASTINT (XCAR (identifierval)) << 16
1072 | XFASTINT (XCDR (identifierval))));
ab552306 1073 return Qnil;
2f65feb6
RS
1074}
1075
ede4db72 1076/* Actually wait for a property change.
2f65feb6 1077 IDENTIFIER should be the value that expect_property_change returned. */
ede4db72
RS
1078
1079static void
d1f21a66
RS
1080wait_for_property_change (location)
1081 struct prop_location *location;
ede4db72 1082{
2f65feb6 1083 int secs, usecs;
aed13378 1084 int count = SPECPDL_INDEX ();
d1f21a66
RS
1085 Lisp_Object tem;
1086
c4898489 1087 tem = Fcons (Qnil, Qnil);
f3fbd155
KR
1088 XSETCARFASTINT (tem, (EMACS_UINT)location >> 16);
1089 XSETCDRFASTINT (tem, (EMACS_UINT)location & 0xffff);
2f65feb6
RS
1090
1091 /* Make sure to do unexpect_property_change if we quit or err. */
d1f21a66 1092 record_unwind_protect (wait_for_property_change_unwind, tem);
2f65feb6 1093
f3fbd155 1094 XSETCAR (property_change_reply, Qnil);
2f65feb6 1095
afe1529d
RS
1096 property_change_reply_object = location;
1097 /* If the event we are waiting for arrives beyond here, it will set
1098 property_change_reply, because property_change_reply_object says so. */
d1f21a66
RS
1099 if (! location->arrived)
1100 {
d1f21a66
RS
1101 secs = x_selection_timeout / 1000;
1102 usecs = (x_selection_timeout % 1000) * 1000;
d9c0d4a3 1103 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
d1f21a66
RS
1104 wait_reading_process_input (secs, usecs, property_change_reply, 0);
1105
8e713be6 1106 if (NILP (XCAR (property_change_reply)))
d9c0d4a3
GM
1107 {
1108 TRACE0 (" Timed out");
1109 error ("Timed out waiting for property-notify event");
1110 }
d1f21a66 1111 }
2f65feb6
RS
1112
1113 unbind_to (count, Qnil);
ede4db72
RS
1114}
1115
1116/* Called from XTread_socket in response to a PropertyNotify event. */
1117
1118void
1119x_handle_property_notify (event)
1120 XPropertyEvent *event;
1121{
1122 struct prop_location *prev = 0, *rest = property_change_wait_list;
d9c0d4a3 1123
ede4db72
RS
1124 while (rest)
1125 {
1126 if (rest->property == event->atom
1127 && rest->window == event->window
1128 && rest->display == event->display
1129 && rest->desired_state == event->state)
1130 {
d9c0d4a3
GM
1131 TRACE2 ("Expected %s of property %s",
1132 (event->state == PropertyDelete ? "deletion" : "change"),
1133 XGetAtomName (event->display, event->atom));
ede4db72 1134
d1f21a66
RS
1135 rest->arrived = 1;
1136
ede4db72
RS
1137 /* If this is the one wait_for_property_change is waiting for,
1138 tell it to wake up. */
d1f21a66 1139 if (rest == property_change_reply_object)
f3fbd155 1140 XSETCAR (property_change_reply, Qt);
ede4db72
RS
1141
1142 if (prev)
1143 prev->next = rest->next;
1144 else
1145 property_change_wait_list = rest->next;
4feb31b2 1146 xfree (rest);
ede4db72
RS
1147 return;
1148 }
1b65481e 1149
ede4db72
RS
1150 prev = rest;
1151 rest = rest->next;
1152 }
ede4db72
RS
1153}
1154
1155
1156\f
1157#if 0 /* #### MULTIPLE doesn't work yet */
1158
1159static Lisp_Object
1160fetch_multiple_target (event)
1161 XSelectionRequestEvent *event;
1162{
1163 Display *display = event->display;
5d0ba25b 1164 Window window = event->requestor;
ede4db72
RS
1165 Atom target = event->target;
1166 Atom selection_atom = event->selection;
1167 int result;
1168
1169 return
1170 Fcons (QMULTIPLE,
1171 x_get_window_property_as_lisp_data (display, window, target,
1172 QMULTIPLE, selection_atom));
1173}
1174
1175static Lisp_Object
1176copy_multiple_data (obj)
1177 Lisp_Object obj;
1178{
1179 Lisp_Object vec;
1180 int i;
1181 int size;
1182 if (CONSP (obj))
8e713be6 1183 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1b65481e 1184
b7826503 1185 CHECK_VECTOR (obj);
ede4db72
RS
1186 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1187 for (i = 0; i < size; i++)
1188 {
1189 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
b7826503 1190 CHECK_VECTOR (vec2);
ede4db72
RS
1191 if (XVECTOR (vec2)->size != 2)
1192 /* ??? Confusing error message */
1193 Fsignal (Qerror, Fcons (build_string ("vectors must be of length 2"),
1194 Fcons (vec2, Qnil)));
1195 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1196 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1197 = XVECTOR (vec2)->contents [0];
1198 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1199 = XVECTOR (vec2)->contents [1];
1200 }
1201 return vec;
1202}
1203
1204#endif
1205
1206\f
1207/* Variables for communication with x_handle_selection_notify. */
1208static Atom reading_which_selection;
1209static Lisp_Object reading_selection_reply;
1210static Window reading_selection_window;
1211
1212/* Do protocol to read selection-data from the server.
1213 Converts this to Lisp data and returns it. */
1214
1215static Lisp_Object
1216x_get_foreign_selection (selection_symbol, target_type)
1217 Lisp_Object selection_symbol, target_type;
1218{
378c33ca
GM
1219 struct frame *sf = SELECTED_FRAME ();
1220 Window requestor_window = FRAME_X_WINDOW (sf);
1221 Display *display = FRAME_X_DISPLAY (sf);
1222 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
5d0ba25b 1223 Time requestor_time = last_event_timestamp;
5c3a351a
RS
1224 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1225 Atom selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
ede4db72 1226 Atom type_atom;
80da0190 1227 int secs, usecs;
5a499696 1228 int count;
55b2d45d 1229 Lisp_Object frame;
ede4db72
RS
1230
1231 if (CONSP (target_type))
8e713be6 1232 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
ede4db72 1233 else
5c3a351a 1234 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
ede4db72
RS
1235
1236 BLOCK_INPUT;
1b65481e 1237
5a499696 1238 count = x_catch_errors (display);
1b65481e 1239
d9c0d4a3
GM
1240 TRACE2 ("Get selection %s, type %s",
1241 XGetAtomName (display, type_atom),
1242 XGetAtomName (display, target_property));
1243
ede4db72 1244 XConvertSelection (display, selection_atom, type_atom, target_property,
5d0ba25b 1245 requestor_window, requestor_time);
5c3a351a 1246 XFlush (display);
ede4db72
RS
1247
1248 /* Prepare to block until the reply has been read. */
5d0ba25b 1249 reading_selection_window = requestor_window;
ede4db72 1250 reading_which_selection = selection_atom;
f3fbd155 1251 XSETCAR (reading_selection_reply, Qnil);
55b2d45d
RS
1252
1253 frame = some_frame_on_display (dpyinfo);
1254
1255 /* If the display no longer has frames, we can't expect
1256 to get many more selection requests from it, so don't
1257 bother trying to queue them. */
1258 if (!NILP (frame))
1259 {
1260 x_start_queuing_selection_requests (display);
1261
1262 record_unwind_protect (queue_selection_requests_unwind,
1263 frame);
1264 }
ede4db72
RS
1265 UNBLOCK_INPUT;
1266
80da0190
RS
1267 /* This allows quits. Also, don't wait forever. */
1268 secs = x_selection_timeout / 1000;
1269 usecs = (x_selection_timeout % 1000) * 1000;
d9c0d4a3 1270 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
80da0190 1271 wait_reading_process_input (secs, usecs, reading_selection_reply, 0);
d9c0d4a3 1272 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
ede4db72 1273
7c6b2ea4 1274 BLOCK_INPUT;
a7b24d46 1275 x_check_errors (display, "Cannot get selection: %s");
5a499696 1276 x_uncatch_errors (display, count);
7c6b2ea4
RS
1277 UNBLOCK_INPUT;
1278
8e713be6 1279 if (NILP (XCAR (reading_selection_reply)))
606140dd 1280 error ("Timed out waiting for reply from selection owner");
8e713be6 1281 if (EQ (XCAR (reading_selection_reply), Qlambda))
d5db4077 1282 error ("No `%s' selection", SDATA (SYMBOL_NAME (selection_symbol)));
ede4db72
RS
1283
1284 /* Otherwise, the selection is waiting for us on the requested property. */
1285 return
5d0ba25b 1286 x_get_window_property_as_lisp_data (display, requestor_window,
ede4db72
RS
1287 target_property, target_type,
1288 selection_atom);
1289}
1290\f
1291/* Subroutines of x_get_window_property_as_lisp_data */
1292
4feb31b2 1293/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1294
ede4db72
RS
1295static void
1296x_get_window_property (display, window, property, data_ret, bytes_ret,
1297 actual_type_ret, actual_format_ret, actual_size_ret,
1298 delete_p)
1299 Display *display;
1300 Window window;
1301 Atom property;
1302 unsigned char **data_ret;
1303 int *bytes_ret;
1304 Atom *actual_type_ret;
1305 int *actual_format_ret;
1306 unsigned long *actual_size_ret;
1307 int delete_p;
1308{
1309 int total_size;
1310 unsigned long bytes_remaining;
1311 int offset = 0;
1312 unsigned char *tmp_data = 0;
1313 int result;
1314 int buffer_size = SELECTION_QUANTUM (display);
1b65481e 1315
d9c0d4a3
GM
1316 if (buffer_size > MAX_SELECTION_QUANTUM)
1317 buffer_size = MAX_SELECTION_QUANTUM;
1b65481e 1318
ede4db72 1319 BLOCK_INPUT;
1b65481e 1320
ede4db72
RS
1321 /* First probe the thing to find out how big it is. */
1322 result = XGetWindowProperty (display, window, property,
137edb72 1323 0L, 0L, False, AnyPropertyType,
ede4db72
RS
1324 actual_type_ret, actual_format_ret,
1325 actual_size_ret,
1326 &bytes_remaining, &tmp_data);
ede4db72
RS
1327 if (result != Success)
1328 {
2f65feb6 1329 UNBLOCK_INPUT;
ede4db72
RS
1330 *data_ret = 0;
1331 *bytes_ret = 0;
1332 return;
1333 }
1b65481e 1334
0158abbc
RS
1335 /* This was allocated by Xlib, so use XFree. */
1336 XFree ((char *) tmp_data);
1b65481e 1337
ede4db72
RS
1338 if (*actual_type_ret == None || *actual_format_ret == 0)
1339 {
2f65feb6 1340 UNBLOCK_INPUT;
ede4db72
RS
1341 return;
1342 }
1343
1344 total_size = bytes_remaining + 1;
1345 *data_ret = (unsigned char *) xmalloc (total_size);
1b65481e 1346
2a1a4c9d 1347 /* Now read, until we've gotten it all. */
ede4db72
RS
1348 while (bytes_remaining)
1349 {
d9c0d4a3 1350#ifdef TRACE_SELECTION
ede4db72
RS
1351 int last = bytes_remaining;
1352#endif
1353 result
1354 = XGetWindowProperty (display, window, property,
137edb72 1355 (long)offset/4, (long)buffer_size/4,
2f65feb6 1356 False,
ede4db72
RS
1357 AnyPropertyType,
1358 actual_type_ret, actual_format_ret,
1359 actual_size_ret, &bytes_remaining, &tmp_data);
d9c0d4a3
GM
1360
1361 TRACE2 ("Read %ld bytes from property %s",
1362 last - bytes_remaining,
1363 XGetAtomName (display, property));
1364
ede4db72
RS
1365 /* If this doesn't return Success at this point, it means that
1366 some clod deleted the selection while we were in the midst of
d9c0d4a3
GM
1367 reading it. Deal with that, I guess.... */
1368 if (result != Success)
1369 break;
ede4db72
RS
1370 *actual_size_ret *= *actual_format_ret / 8;
1371 bcopy (tmp_data, (*data_ret) + offset, *actual_size_ret);
1372 offset += *actual_size_ret;
1b65481e 1373
0158abbc
RS
1374 /* This was allocated by Xlib, so use XFree. */
1375 XFree ((char *) tmp_data);
ede4db72 1376 }
2f65feb6 1377
5c3a351a 1378 XFlush (display);
ede4db72
RS
1379 UNBLOCK_INPUT;
1380 *bytes_ret = offset;
1381}
1382\f
4feb31b2 1383/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1384
ede4db72
RS
1385static void
1386receive_incremental_selection (display, window, property, target_type,
1387 min_size_bytes, data_ret, size_bytes_ret,
1388 type_ret, format_ret, size_ret)
1389 Display *display;
1390 Window window;
1391 Atom property;
1392 Lisp_Object target_type; /* for error messages only */
1393 unsigned int min_size_bytes;
1394 unsigned char **data_ret;
1395 int *size_bytes_ret;
1396 Atom *type_ret;
1397 unsigned long *size_ret;
1398 int *format_ret;
1399{
1400 int offset = 0;
d1f21a66 1401 struct prop_location *wait_object;
ede4db72
RS
1402 *size_bytes_ret = min_size_bytes;
1403 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
d9c0d4a3
GM
1404
1405 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
2f65feb6
RS
1406
1407 /* At this point, we have read an INCR property.
1408 Delete the property to ack it.
1409 (But first, prepare to receive the next event in this handshake.)
ede4db72
RS
1410
1411 Now, we must loop, waiting for the sending window to put a value on
1412 that property, then reading the property, then deleting it to ack.
1413 We are done when the sender places a property of length 0.
1414 */
2f65feb6
RS
1415 BLOCK_INPUT;
1416 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
d9c0d4a3
GM
1417 TRACE1 (" Delete property %s",
1418 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
2f65feb6 1419 XDeleteProperty (display, window, property);
d9c0d4a3
GM
1420 TRACE1 (" Expect new value of property %s",
1421 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
d1f21a66
RS
1422 wait_object = expect_property_change (display, window, property,
1423 PropertyNewValue);
5c3a351a 1424 XFlush (display);
2f65feb6
RS
1425 UNBLOCK_INPUT;
1426
ede4db72
RS
1427 while (1)
1428 {
1429 unsigned char *tmp_data;
1430 int tmp_size_bytes;
d9c0d4a3
GM
1431
1432 TRACE0 (" Wait for property change");
d1f21a66 1433 wait_for_property_change (wait_object);
1b65481e 1434
ede4db72 1435 /* expect it again immediately, because x_get_window_property may
2a1a4c9d 1436 .. no it won't, I don't get it.
d9c0d4a3
GM
1437 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1438 TRACE0 (" Get property value");
ede4db72
RS
1439 x_get_window_property (display, window, property,
1440 &tmp_data, &tmp_size_bytes,
1441 type_ret, format_ret, size_ret, 1);
1442
d9c0d4a3
GM
1443 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1444
ede4db72
RS
1445 if (tmp_size_bytes == 0) /* we're done */
1446 {
d9c0d4a3
GM
1447 TRACE0 ("Done reading incrementally");
1448
2f65feb6
RS
1449 if (! waiting_for_other_props_on_window (display, window))
1450 XSelectInput (display, window, STANDARD_EVENT_SET);
d1f21a66 1451 unexpect_property_change (wait_object);
4feb31b2 1452 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1453 calls xmalloc itself. */
4feb31b2 1454 if (tmp_data) xfree (tmp_data);
ede4db72
RS
1455 break;
1456 }
2f65feb6
RS
1457
1458 BLOCK_INPUT;
d9c0d4a3
GM
1459 TRACE1 (" ACK by deleting property %s",
1460 XGetAtomName (display, property));
2f65feb6 1461 XDeleteProperty (display, window, property);
d1f21a66
RS
1462 wait_object = expect_property_change (display, window, property,
1463 PropertyNewValue);
5c3a351a 1464 XFlush (display);
2f65feb6
RS
1465 UNBLOCK_INPUT;
1466
ede4db72
RS
1467 if (*size_bytes_ret < offset + tmp_size_bytes)
1468 {
ede4db72
RS
1469 *size_bytes_ret = offset + tmp_size_bytes;
1470 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1471 }
1b65481e 1472
018cfa07 1473 bcopy (tmp_data, (*data_ret) + offset, tmp_size_bytes);
ede4db72 1474 offset += tmp_size_bytes;
1b65481e 1475
4feb31b2 1476 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1477 calls xmalloc itself. */
4feb31b2 1478 xfree (tmp_data);
ede4db72
RS
1479 }
1480}
d9c0d4a3 1481
ede4db72
RS
1482\f
1483/* Once a requested selection is "ready" (we got a SelectionNotify event),
1484 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1485 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1486
1487static Lisp_Object
1488x_get_window_property_as_lisp_data (display, window, property, target_type,
1489 selection_atom)
1490 Display *display;
1491 Window window;
1492 Atom property;
1493 Lisp_Object target_type; /* for error messages only */
1494 Atom selection_atom; /* for error messages only */
1495{
1496 Atom actual_type;
1497 int actual_format;
1498 unsigned long actual_size;
1499 unsigned char *data = 0;
1500 int bytes = 0;
1501 Lisp_Object val;
5c3a351a 1502 struct x_display_info *dpyinfo = x_display_info_for_display (display);
ede4db72 1503
d9c0d4a3
GM
1504 TRACE0 ("Reading selection data");
1505
ede4db72
RS
1506 x_get_window_property (display, window, property, &data, &bytes,
1507 &actual_type, &actual_format, &actual_size, 1);
1508 if (! data)
1509 {
1510 int there_is_a_selection_owner;
1511 BLOCK_INPUT;
1512 there_is_a_selection_owner
1513 = XGetSelectionOwner (display, selection_atom);
1514 UNBLOCK_INPUT;
5a499696
RS
1515 Fsignal (Qerror,
1516 there_is_a_selection_owner
1517 ? Fcons (build_string ("selection owner couldn't convert"),
ede4db72
RS
1518 actual_type
1519 ? Fcons (target_type,
d9c0d4a3 1520 Fcons (x_atom_to_symbol (display,
5c3a351a 1521 actual_type),
ede4db72
RS
1522 Qnil))
1523 : Fcons (target_type, Qnil))
5a499696 1524 : Fcons (build_string ("no selection"),
d9c0d4a3 1525 Fcons (x_atom_to_symbol (display,
5a499696
RS
1526 selection_atom),
1527 Qnil)));
ede4db72 1528 }
1b65481e 1529
5c3a351a 1530 if (actual_type == dpyinfo->Xatom_INCR)
ede4db72
RS
1531 {
1532 /* That wasn't really the data, just the beginning. */
1533
1534 unsigned int min_size_bytes = * ((unsigned int *) data);
1535 BLOCK_INPUT;
4feb31b2 1536 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1537 calls xmalloc itself. */
4feb31b2 1538 xfree ((char *) data);
ede4db72
RS
1539 UNBLOCK_INPUT;
1540 receive_incremental_selection (display, window, property, target_type,
1541 min_size_bytes, &data, &bytes,
1542 &actual_type, &actual_format,
1543 &actual_size);
1544 }
1545
2f65feb6 1546 BLOCK_INPUT;
d9c0d4a3 1547 TRACE1 (" Delete property %s", XGetAtomName (display, property));
2f65feb6 1548 XDeleteProperty (display, window, property);
5c3a351a 1549 XFlush (display);
2f65feb6
RS
1550 UNBLOCK_INPUT;
1551
ede4db72
RS
1552 /* It's been read. Now convert it to a lisp object in some semi-rational
1553 manner. */
1554 val = selection_data_to_lisp_data (display, data, bytes,
1555 actual_type, actual_format);
1b65481e 1556
4feb31b2 1557 /* Use xfree, not XFree, because x_get_window_property
0158abbc 1558 calls xmalloc itself. */
4feb31b2 1559 xfree ((char *) data);
ede4db72
RS
1560 return val;
1561}
1562\f
1563/* These functions convert from the selection data read from the server into
1564 something that we can use from Lisp, and vice versa.
1565
1566 Type: Format: Size: Lisp Type:
1567 ----- ------- ----- -----------
1568 * 8 * String
1569 ATOM 32 1 Symbol
1570 ATOM 32 > 1 Vector of Symbols
1571 * 16 1 Integer
1572 * 16 > 1 Vector of Integers
1573 * 32 1 if <=16 bits: Integer
1574 if > 16 bits: Cons of top16, bot16
1575 * 32 > 1 Vector of the above
1576
1577 When converting a Lisp number to C, it is assumed to be of format 16 if
1578 it is an integer, and of format 32 if it is a cons of two integers.
1579
1580 When converting a vector of numbers from Lisp to C, it is assumed to be
1581 of format 16 if every element in the vector is an integer, and is assumed
1582 to be of format 32 if any element is a cons of two integers.
1583
1584 When converting an object to C, it may be of the form (SYMBOL . <data>)
1585 where SYMBOL is what we should claim that the type is. Format and
1586 representation are as above. */
1587
1588
1589
1590static Lisp_Object
1591selection_data_to_lisp_data (display, data, size, type, format)
1592 Display *display;
1593 unsigned char *data;
1594 Atom type;
1595 int size, format;
1596{
5c3a351a 1597 struct x_display_info *dpyinfo = x_display_info_for_display (display);
ede4db72 1598
5c3a351a 1599 if (type == dpyinfo->Xatom_NULL)
ede4db72
RS
1600 return QNULL;
1601
1602 /* Convert any 8-bit data to a string, for compactness. */
1603 else if (format == 8)
e6c7c988
KH
1604 {
1605 Lisp_Object str;
f78da51e
KH
1606 int require_encoding = 0;
1607
d24e26b1
RS
1608 if (
1609#if 1
1610 1
1611#else
1612 ! NILP (buffer_defaults.enable_multibyte_characters)
1613#endif
1614 )
f78da51e 1615 {
5faa9b45
KH
1616 /* If TYPE is `TEXT' or `COMPOUND_TEXT', we should decode
1617 DATA to Emacs internal format because DATA may be encoded
1618 in compound text format. In addtion, if TYPE is `STRING'
1619 and DATA contains any 8-bit Latin-1 code, we should also
1620 decode it. */
1621 if (type == dpyinfo->Xatom_TEXT
1622 || type == dpyinfo->Xatom_COMPOUND_TEXT)
1623 require_encoding = 1;
1624 else if (type == XA_STRING)
f78da51e 1625 {
5faa9b45
KH
1626 int i;
1627 for (i = 0; i < size; i++)
f78da51e 1628 {
5faa9b45
KH
1629 if (data[i] >= 0x80)
1630 {
1631 require_encoding = 1;
1632 break;
1633 }
f78da51e
KH
1634 }
1635 }
1636 }
1637 if (!require_encoding)
061bda8d
KH
1638 {
1639 str = make_unibyte_string ((char *) data, size);
1640 Vlast_coding_system_used = Qraw_text;
1641 }
e6c7c988
KH
1642 else
1643 {
9391d06a 1644 int bufsize;
e6c7c988
KH
1645 unsigned char *buf;
1646 struct coding_system coding;
e6c7c988 1647
16cd5029
KH
1648 if (NILP (Vnext_selection_coding_system))
1649 Vnext_selection_coding_system = Vselection_coding_system;
2584c9ec 1650 setup_coding_system
16cd5029 1651 (Fcheck_coding_system(Vnext_selection_coding_system), &coding);
9846d9db
KH
1652 coding.src_multibyte = 0;
1653 coding.dst_multibyte = 1;
16cd5029 1654 Vnext_selection_coding_system = Qnil;
9391d06a 1655 coding.mode |= CODING_MODE_LAST_BLOCK;
e1885335
KH
1656 /* We explicitely disable composition handling because
1657 selection data should not contain any composition
1658 sequence. */
1659 coding.composing = COMPOSITION_DISABLED;
e6c7c988
KH
1660 bufsize = decoding_buffer_size (&coding, size);
1661 buf = (unsigned char *) xmalloc (bufsize);
9391d06a 1662 decode_coding (&coding, data, buf, size, bufsize);
9846d9db
KH
1663 str = make_string_from_bytes ((char *) buf,
1664 coding.produced_char, coding.produced);
4feb31b2 1665 xfree (buf);
fbbe0ace
EZ
1666
1667 if (SYMBOLP (coding.post_read_conversion)
1668 && !NILP (Ffboundp (coding.post_read_conversion)))
ecb7ac21 1669 str = run_pre_post_conversion_on_str (str, &coding, 0);
061bda8d 1670 Vlast_coding_system_used = coding.symbol;
e6c7c988 1671 }
d5db4077 1672 compose_chars_in_text (0, SCHARS (str), str);
e6c7c988
KH
1673 return str;
1674 }
ede4db72
RS
1675 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1676 a vector of symbols.
1677 */
1678 else if (type == XA_ATOM)
1679 {
1680 int i;
1681 if (size == sizeof (Atom))
d9c0d4a3 1682 return x_atom_to_symbol (display, *((Atom *) data));
ede4db72
RS
1683 else
1684 {
e607a484
RS
1685 Lisp_Object v = Fmake_vector (make_number (size / sizeof (Atom)),
1686 make_number (0));
ede4db72 1687 for (i = 0; i < size / sizeof (Atom); i++)
e607a484 1688 Faset (v, make_number (i),
d9c0d4a3 1689 x_atom_to_symbol (display, ((Atom *) data) [i]));
ede4db72
RS
1690 return v;
1691 }
1692 }
1693
1694 /* Convert a single 16 or small 32 bit number to a Lisp_Int.
1695 If the number is > 16 bits, convert it to a cons of integers,
1696 16 bits in each half.
1697 */
1698 else if (format == 32 && size == sizeof (long))
1699 return long_to_cons (((unsigned long *) data) [0]);
1700 else if (format == 16 && size == sizeof (short))
1701 return make_number ((int) (((unsigned short *) data) [0]));
1702
1703 /* Convert any other kind of data to a vector of numbers, represented
1704 as above (as an integer, or a cons of two 16 bit integers.)
1705 */
1706 else if (format == 16)
1707 {
1708 int i;
937a3875
RS
1709 Lisp_Object v;
1710 v = Fmake_vector (make_number (size / 2), make_number (0));
1711 for (i = 0; i < size / 2; i++)
ede4db72
RS
1712 {
1713 int j = (int) ((unsigned short *) data) [i];
e607a484 1714 Faset (v, make_number (i), make_number (j));
ede4db72
RS
1715 }
1716 return v;
1717 }
1718 else
1719 {
1720 int i;
e607a484 1721 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
ede4db72
RS
1722 for (i = 0; i < size / 4; i++)
1723 {
1724 unsigned long j = ((unsigned long *) data) [i];
e607a484 1725 Faset (v, make_number (i), long_to_cons (j));
ede4db72
RS
1726 }
1727 return v;
1728 }
1729}
1730
1731
4feb31b2 1732/* Use xfree, not XFree, to free the data obtained with this function. */
0158abbc 1733
ede4db72
RS
1734static void
1735lisp_data_to_selection_data (display, obj,
aca39f42
RS
1736 data_ret, type_ret, size_ret,
1737 format_ret, nofree_ret)
ede4db72
RS
1738 Display *display;
1739 Lisp_Object obj;
1740 unsigned char **data_ret;
1741 Atom *type_ret;
1742 unsigned int *size_ret;
1743 int *format_ret;
aca39f42 1744 int *nofree_ret;
ede4db72
RS
1745{
1746 Lisp_Object type = Qnil;
5c3a351a 1747 struct x_display_info *dpyinfo = x_display_info_for_display (display);
aca39f42
RS
1748
1749 *nofree_ret = 0;
1750
8e713be6 1751 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
ede4db72 1752 {
8e713be6
KR
1753 type = XCAR (obj);
1754 obj = XCDR (obj);
1755 if (CONSP (obj) && NILP (XCDR (obj)))
1756 obj = XCAR (obj);
ede4db72
RS
1757 }
1758
1759 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1760 { /* This is not the same as declining */
1761 *format_ret = 32;
1762 *size_ret = 0;
1763 *data_ret = 0;
1764 type = QNULL;
1765 }
1766 else if (STRINGP (obj))
1767 {
5109c8dd 1768 xassert (! STRING_MULTIBYTE (obj));
7b9ae523 1769 if (NILP (type))
5109c8dd
KH
1770 type = QSTRING;
1771 *format_ret = 8;
1772 *size_ret = SBYTES (obj);
1773 *data_ret = SDATA (obj);
1774 *nofree_ret = 1;
ede4db72
RS
1775 }
1776 else if (SYMBOLP (obj))
1777 {
1778 *format_ret = 32;
1779 *size_ret = 1;
1780 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1781 (*data_ret) [sizeof (Atom)] = 0;
5c3a351a 1782 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
ede4db72
RS
1783 if (NILP (type)) type = QATOM;
1784 }
7da64e5c 1785 else if (INTEGERP (obj)
ede4db72
RS
1786 && XINT (obj) < 0xFFFF
1787 && XINT (obj) > -0xFFFF)
1788 {
1789 *format_ret = 16;
1790 *size_ret = 1;
1791 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1792 (*data_ret) [sizeof (short)] = 0;
1793 (*(short **) data_ret) [0] = (short) XINT (obj);
1794 if (NILP (type)) type = QINTEGER;
1795 }
a87ed99c 1796 else if (INTEGERP (obj)
8e713be6
KR
1797 || (CONSP (obj) && INTEGERP (XCAR (obj))
1798 && (INTEGERP (XCDR (obj))
1799 || (CONSP (XCDR (obj))
1800 && INTEGERP (XCAR (XCDR (obj)))))))
ede4db72
RS
1801 {
1802 *format_ret = 32;
1803 *size_ret = 1;
1804 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1805 (*data_ret) [sizeof (long)] = 0;
1806 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1807 if (NILP (type)) type = QINTEGER;
1808 }
1809 else if (VECTORP (obj))
1810 {
1811 /* Lisp_Vectors may represent a set of ATOMs;
1812 a set of 16 or 32 bit INTEGERs;
1813 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1814 */
1815 int i;
1816
1817 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1818 /* This vector is an ATOM set */
1819 {
1820 if (NILP (type)) type = QATOM;
1821 *size_ret = XVECTOR (obj)->size;
1822 *format_ret = 32;
1823 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1824 for (i = 0; i < *size_ret; i++)
1825 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1826 (*(Atom **) data_ret) [i]
5c3a351a 1827 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
ede4db72
RS
1828 else
1829 Fsignal (Qerror, /* Qselection_error */
1830 Fcons (build_string
1831 ("all elements of selection vector must have same type"),
1832 Fcons (obj, Qnil)));
1833 }
1834#if 0 /* #### MULTIPLE doesn't work yet */
1835 else if (VECTORP (XVECTOR (obj)->contents [0]))
1836 /* This vector is an ATOM_PAIR set */
1837 {
1838 if (NILP (type)) type = QATOM_PAIR;
1839 *size_ret = XVECTOR (obj)->size;
1840 *format_ret = 32;
1841 *data_ret = (unsigned char *)
1842 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1843 for (i = 0; i < *size_ret; i++)
1844 if (VECTORP (XVECTOR (obj)->contents [i]))
1845 {
1846 Lisp_Object pair = XVECTOR (obj)->contents [i];
1847 if (XVECTOR (pair)->size != 2)
1848 Fsignal (Qerror,
1b65481e 1849 Fcons (build_string
ede4db72
RS
1850 ("elements of the vector must be vectors of exactly two elements"),
1851 Fcons (pair, Qnil)));
1b65481e 1852
ede4db72 1853 (*(Atom **) data_ret) [i * 2]
5c3a351a
RS
1854 = symbol_to_x_atom (dpyinfo, display,
1855 XVECTOR (pair)->contents [0]);
ede4db72 1856 (*(Atom **) data_ret) [(i * 2) + 1]
5c3a351a
RS
1857 = symbol_to_x_atom (dpyinfo, display,
1858 XVECTOR (pair)->contents [1]);
ede4db72
RS
1859 }
1860 else
1861 Fsignal (Qerror,
1862 Fcons (build_string
1863 ("all elements of the vector must be of the same type"),
1864 Fcons (obj, Qnil)));
1b65481e 1865
ede4db72
RS
1866 }
1867#endif
1868 else
1869 /* This vector is an INTEGER set, or something like it */
1870 {
1871 *size_ret = XVECTOR (obj)->size;
1872 if (NILP (type)) type = QINTEGER;
1873 *format_ret = 16;
1874 for (i = 0; i < *size_ret; i++)
1875 if (CONSP (XVECTOR (obj)->contents [i]))
1876 *format_ret = 32;
7da64e5c 1877 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
ede4db72
RS
1878 Fsignal (Qerror, /* Qselection_error */
1879 Fcons (build_string
1880 ("elements of selection vector must be integers or conses of integers"),
1881 Fcons (obj, Qnil)));
1882
1883 *data_ret = (unsigned char *) xmalloc (*size_ret * (*format_ret/8));
1884 for (i = 0; i < *size_ret; i++)
1885 if (*format_ret == 32)
1886 (*((unsigned long **) data_ret)) [i]
1887 = cons_to_long (XVECTOR (obj)->contents [i]);
1888 else
1889 (*((unsigned short **) data_ret)) [i]
1890 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1891 }
1892 }
1893 else
1894 Fsignal (Qerror, /* Qselection_error */
1895 Fcons (build_string ("unrecognised selection data"),
1896 Fcons (obj, Qnil)));
1897
5c3a351a 1898 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
ede4db72
RS
1899}
1900
1901static Lisp_Object
1902clean_local_selection_data (obj)
1903 Lisp_Object obj;
1904{
1905 if (CONSP (obj)
8e713be6
KR
1906 && INTEGERP (XCAR (obj))
1907 && CONSP (XCDR (obj))
1908 && INTEGERP (XCAR (XCDR (obj)))
1909 && NILP (XCDR (XCDR (obj))))
1910 obj = Fcons (XCAR (obj), XCDR (obj));
ede4db72
RS
1911
1912 if (CONSP (obj)
8e713be6
KR
1913 && INTEGERP (XCAR (obj))
1914 && INTEGERP (XCDR (obj)))
ede4db72 1915 {
8e713be6
KR
1916 if (XINT (XCAR (obj)) == 0)
1917 return XCDR (obj);
1918 if (XINT (XCAR (obj)) == -1)
1919 return make_number (- XINT (XCDR (obj)));
ede4db72
RS
1920 }
1921 if (VECTORP (obj))
1922 {
1923 int i;
1924 int size = XVECTOR (obj)->size;
1925 Lisp_Object copy;
1926 if (size == 1)
1927 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
e607a484 1928 copy = Fmake_vector (make_number (size), Qnil);
ede4db72
RS
1929 for (i = 0; i < size; i++)
1930 XVECTOR (copy)->contents [i]
1931 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1932 return copy;
1933 }
1934 return obj;
1935}
1936\f
1937/* Called from XTread_socket to handle SelectionNotify events.
606140dd
KH
1938 If it's the selection we are waiting for, stop waiting
1939 by setting the car of reading_selection_reply to non-nil.
1940 We store t there if the reply is successful, lambda if not. */
ede4db72
RS
1941
1942void
1943x_handle_selection_notify (event)
1944 XSelectionEvent *event;
1945{
5d0ba25b 1946 if (event->requestor != reading_selection_window)
ede4db72
RS
1947 return;
1948 if (event->selection != reading_which_selection)
1949 return;
1950
d9c0d4a3 1951 TRACE0 ("Received SelectionNotify");
f3fbd155
KR
1952 XSETCAR (reading_selection_reply,
1953 (event->property != 0 ? Qt : Qlambda));
ede4db72
RS
1954}
1955
1956\f
a0d76c27 1957DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
8c1a1077
PJ
1958 Sx_own_selection_internal, 2, 2, 0,
1959 doc: /* Assert an X selection of the given TYPE with the given VALUE.
1960TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1961\(Those are literal upper-case symbol names, since that's what X expects.)
1962VALUE is typically a string, or a cons of two markers, but may be
1963anything that the functions on `selection-converter-alist' know about. */)
1964 (selection_name, selection_value)
ede4db72
RS
1965 Lisp_Object selection_name, selection_value;
1966{
703e0710 1967 check_x ();
b7826503 1968 CHECK_SYMBOL (selection_name);
606140dd 1969 if (NILP (selection_value)) error ("selection-value may not be nil");
ede4db72
RS
1970 x_own_selection (selection_name, selection_value);
1971 return selection_value;
1972}
1973
1974
1975/* Request the selection value from the owner. If we are the owner,
1976 simply return our selection value. If we are not the owner, this
1977 will block until all of the data has arrived. */
1978
a0d76c27 1979DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
8c1a1077
PJ
1980 Sx_get_selection_internal, 2, 2, 0,
1981 doc: /* Return text selected from some X window.
1982SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1983\(Those are literal upper-case symbol names, since that's what X expects.)
1984TYPE is the type of data desired, typically `STRING'. */)
1985 (selection_symbol, target_type)
ede4db72
RS
1986 Lisp_Object selection_symbol, target_type;
1987{
1988 Lisp_Object val = Qnil;
1989 struct gcpro gcpro1, gcpro2;
1990 GCPRO2 (target_type, val); /* we store newly consed data into these */
703e0710 1991 check_x ();
b7826503 1992 CHECK_SYMBOL (selection_symbol);
ede4db72
RS
1993
1994#if 0 /* #### MULTIPLE doesn't work yet */
1995 if (CONSP (target_type)
8e713be6 1996 && XCAR (target_type) == QMULTIPLE)
ede4db72 1997 {
b7826503 1998 CHECK_VECTOR (XCDR (target_type));
ede4db72
RS
1999 /* So we don't destructively modify this... */
2000 target_type = copy_multiple_data (target_type);
2001 }
2002 else
2003#endif
b7826503 2004 CHECK_SYMBOL (target_type);
ede4db72 2005
5109c8dd 2006 val = x_get_local_selection (selection_symbol, target_type, 1);
ede4db72
RS
2007
2008 if (NILP (val))
2009 {
2010 val = x_get_foreign_selection (selection_symbol, target_type);
2011 goto DONE;
2012 }
2013
2014 if (CONSP (val)
8e713be6 2015 && SYMBOLP (XCAR (val)))
ede4db72 2016 {
8e713be6
KR
2017 val = XCDR (val);
2018 if (CONSP (val) && NILP (XCDR (val)))
2019 val = XCAR (val);
ede4db72
RS
2020 }
2021 val = clean_local_selection_data (val);
2022 DONE:
2023 UNGCPRO;
2024 return val;
2025}
2026
a0d76c27 2027DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
8c1a1077
PJ
2028 Sx_disown_selection_internal, 1, 2, 0,
2029 doc: /* If we own the selection SELECTION, disown it.
2030Disowning it means there is no such selection. */)
2031 (selection, time)
ede4db72
RS
2032 Lisp_Object selection;
2033 Lisp_Object time;
2034{
ede4db72
RS
2035 Time timestamp;
2036 Atom selection_atom;
44d46531 2037 struct selection_input_event event;
3834c318 2038 Display *display;
5c3a351a 2039 struct x_display_info *dpyinfo;
378c33ca 2040 struct frame *sf = SELECTED_FRAME ();
ede4db72 2041
703e0710 2042 check_x ();
378c33ca
GM
2043 display = FRAME_X_DISPLAY (sf);
2044 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
b7826503 2045 CHECK_SYMBOL (selection);
ede4db72 2046 if (NILP (time))
7da64e5c 2047 timestamp = last_event_timestamp;
ede4db72
RS
2048 else
2049 timestamp = cons_to_long (time);
2050
2051 if (NILP (assq_no_quit (selection, Vselection_alist)))
2052 return Qnil; /* Don't disown the selection when we're not the owner. */
2053
5c3a351a 2054 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
ede4db72
RS
2055
2056 BLOCK_INPUT;
2057 XSetSelectionOwner (display, selection_atom, None, timestamp);
2058 UNBLOCK_INPUT;
2059
eb8c3be9 2060 /* It doesn't seem to be guaranteed that a SelectionClear event will be
ede4db72
RS
2061 generated for a window which owns the selection when that window sets
2062 the selection owner to None. The NCD server does, the MIT Sun4 server
2063 doesn't. So we synthesize one; this means we might get two, but
2064 that's ok, because the second one won't have any effect. */
8d47f8c4
RS
2065 SELECTION_EVENT_DISPLAY (&event) = display;
2066 SELECTION_EVENT_SELECTION (&event) = selection_atom;
2067 SELECTION_EVENT_TIME (&event) = timestamp;
44d46531 2068 x_handle_selection_clear ((struct input_event *) &event);
ede4db72
RS
2069
2070 return Qt;
2071}
2072
a87ed99c
RS
2073/* Get rid of all the selections in buffer BUFFER.
2074 This is used when we kill a buffer. */
2075
2076void
2077x_disown_buffer_selections (buffer)
2078 Lisp_Object buffer;
2079{
2080 Lisp_Object tail;
2081 struct buffer *buf = XBUFFER (buffer);
2082
8e713be6 2083 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
a87ed99c
RS
2084 {
2085 Lisp_Object elt, value;
8e713be6
KR
2086 elt = XCAR (tail);
2087 value = XCDR (elt);
2088 if (CONSP (value) && MARKERP (XCAR (value))
2089 && XMARKER (XCAR (value))->buffer == buf)
2090 Fx_disown_selection_internal (XCAR (elt), Qnil);
a87ed99c
RS
2091 }
2092}
ede4db72
RS
2093
2094DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
8c1a1077
PJ
2095 0, 1, 0,
2096 doc: /* Whether the current Emacs process owns the given X Selection.
2097The arg should be the name of the selection in question, typically one of
2098the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2099\(Those are literal upper-case symbol names, since that's what X expects.)
2100For convenience, the symbol nil is the same as `PRIMARY',
2101and t is the same as `SECONDARY'. */)
2102 (selection)
ede4db72
RS
2103 Lisp_Object selection;
2104{
703e0710 2105 check_x ();
b7826503 2106 CHECK_SYMBOL (selection);
ede4db72
RS
2107 if (EQ (selection, Qnil)) selection = QPRIMARY;
2108 if (EQ (selection, Qt)) selection = QSECONDARY;
1b65481e 2109
ede4db72
RS
2110 if (NILP (Fassq (selection, Vselection_alist)))
2111 return Qnil;
2112 return Qt;
2113}
2114
2115DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
8c1a1077
PJ
2116 0, 1, 0,
2117 doc: /* Whether there is an owner for the given X Selection.
2118The arg should be the name of the selection in question, typically one of
2119the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2120\(Those are literal upper-case symbol names, since that's what X expects.)
2121For convenience, the symbol nil is the same as `PRIMARY',
2122and t is the same as `SECONDARY'. */)
2123 (selection)
ede4db72
RS
2124 Lisp_Object selection;
2125{
2126 Window owner;
356ba514 2127 Atom atom;
3834c318 2128 Display *dpy;
378c33ca 2129 struct frame *sf = SELECTED_FRAME ();
3834c318 2130
b8c70430 2131 /* It should be safe to call this before we have an X frame. */
378c33ca 2132 if (! FRAME_X_P (sf))
b8c70430
RS
2133 return Qnil;
2134
378c33ca 2135 dpy = FRAME_X_DISPLAY (sf);
b7826503 2136 CHECK_SYMBOL (selection);
ede4db72
RS
2137 if (!NILP (Fx_selection_owner_p (selection)))
2138 return Qt;
356ba514
RS
2139 if (EQ (selection, Qnil)) selection = QPRIMARY;
2140 if (EQ (selection, Qt)) selection = QSECONDARY;
378c33ca 2141 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
356ba514
RS
2142 if (atom == 0)
2143 return Qnil;
ede4db72 2144 BLOCK_INPUT;
356ba514 2145 owner = XGetSelectionOwner (dpy, atom);
ede4db72
RS
2146 UNBLOCK_INPUT;
2147 return (owner ? Qt : Qnil);
2148}
2149
2150\f
2151#ifdef CUT_BUFFER_SUPPORT
2152
ede4db72
RS
2153/* Ensure that all 8 cut buffers exist. ICCCM says we gotta... */
2154static void
2155initialize_cut_buffers (display, window)
2156 Display *display;
2157 Window window;
2158{
2159 unsigned char *data = (unsigned char *) "";
2160 BLOCK_INPUT;
2161#define FROB(atom) XChangeProperty (display, window, atom, XA_STRING, 8, \
2162 PropModeAppend, data, 0)
2163 FROB (XA_CUT_BUFFER0);
2164 FROB (XA_CUT_BUFFER1);
2165 FROB (XA_CUT_BUFFER2);
2166 FROB (XA_CUT_BUFFER3);
2167 FROB (XA_CUT_BUFFER4);
2168 FROB (XA_CUT_BUFFER5);
2169 FROB (XA_CUT_BUFFER6);
2170 FROB (XA_CUT_BUFFER7);
2171#undef FROB
2172 UNBLOCK_INPUT;
ede4db72
RS
2173}
2174
2175
b7826503
PJ
2176#define CHECK_CUT_BUFFER(symbol) \
2177 { CHECK_SYMBOL ((symbol)); \
ede4db72
RS
2178 if (!EQ((symbol), QCUT_BUFFER0) && !EQ((symbol), QCUT_BUFFER1) \
2179 && !EQ((symbol), QCUT_BUFFER2) && !EQ((symbol), QCUT_BUFFER3) \
2180 && !EQ((symbol), QCUT_BUFFER4) && !EQ((symbol), QCUT_BUFFER5) \
2181 && !EQ((symbol), QCUT_BUFFER6) && !EQ((symbol), QCUT_BUFFER7)) \
2182 Fsignal (Qerror, \
a87ed99c 2183 Fcons (build_string ("doesn't name a cut buffer"), \
ede4db72
RS
2184 Fcons ((symbol), Qnil))); \
2185 }
2186
a87ed99c 2187DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
8c1a1077
PJ
2188 Sx_get_cut_buffer_internal, 1, 1, 0,
2189 doc: /* Returns the value of the named cut buffer (typically CUT_BUFFER0). */)
2190 (buffer)
ede4db72
RS
2191 Lisp_Object buffer;
2192{
3834c318 2193 Window window;
ede4db72
RS
2194 Atom buffer_atom;
2195 unsigned char *data;
2196 int bytes;
2197 Atom type;
2198 int format;
2199 unsigned long size;
2200 Lisp_Object ret;
3834c318 2201 Display *display;
5c3a351a 2202 struct x_display_info *dpyinfo;
378c33ca 2203 struct frame *sf = SELECTED_FRAME ();
ede4db72 2204
703e0710 2205 check_x ();
378c33ca
GM
2206 display = FRAME_X_DISPLAY (sf);
2207 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
3834c318 2208 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
b7826503 2209 CHECK_CUT_BUFFER (buffer);
5c3a351a 2210 buffer_atom = symbol_to_x_atom (dpyinfo, display, buffer);
ede4db72
RS
2211
2212 x_get_window_property (display, window, buffer_atom, &data, &bytes,
2213 &type, &format, &size, 0);
22e00739
RS
2214 if (!data || !format)
2215 return Qnil;
1b65481e 2216
ede4db72
RS
2217 if (format != 8 || type != XA_STRING)
2218 Fsignal (Qerror,
2219 Fcons (build_string ("cut buffer doesn't contain 8-bit data"),
d9c0d4a3 2220 Fcons (x_atom_to_symbol (display, type),
ede4db72
RS
2221 Fcons (make_number (format), Qnil))));
2222
2223 ret = (bytes ? make_string ((char *) data, bytes) : Qnil);
4feb31b2 2224 /* Use xfree, not XFree, because x_get_window_property
0158abbc 2225 calls xmalloc itself. */
4feb31b2 2226 xfree (data);
ede4db72
RS
2227 return ret;
2228}
2229
2230
a87ed99c 2231DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
8c1a1077
PJ
2232 Sx_store_cut_buffer_internal, 2, 2, 0,
2233 doc: /* Sets the value of the named cut buffer (typically CUT_BUFFER0). */)
2234 (buffer, string)
ede4db72
RS
2235 Lisp_Object buffer, string;
2236{
3834c318 2237 Window window;
ede4db72
RS
2238 Atom buffer_atom;
2239 unsigned char *data;
2240 int bytes;
2241 int bytes_remaining;
3834c318
RS
2242 int max_bytes;
2243 Display *display;
378c33ca 2244 struct frame *sf = SELECTED_FRAME ();
ede4db72 2245
703e0710 2246 check_x ();
378c33ca 2247 display = FRAME_X_DISPLAY (sf);
3834c318
RS
2248 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2249
2250 max_bytes = SELECTION_QUANTUM (display);
2251 if (max_bytes > MAX_SELECTION_QUANTUM)
2252 max_bytes = MAX_SELECTION_QUANTUM;
2253
b7826503
PJ
2254 CHECK_CUT_BUFFER (buffer);
2255 CHECK_STRING (string);
378c33ca 2256 buffer_atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf),
5c3a351a 2257 display, buffer);
d5db4077
KR
2258 data = (unsigned char *) SDATA (string);
2259 bytes = SBYTES (string);
ede4db72
RS
2260 bytes_remaining = bytes;
2261
378c33ca 2262 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
04649dbc
KH
2263 {
2264 initialize_cut_buffers (display, window);
378c33ca 2265 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
04649dbc 2266 }
ede4db72
RS
2267
2268 BLOCK_INPUT;
10608c8c
RS
2269
2270 /* Don't mess up with an empty value. */
2271 if (!bytes_remaining)
2272 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2273 PropModeReplace, data, 0);
2274
ede4db72
RS
2275 while (bytes_remaining)
2276 {
2277 int chunk = (bytes_remaining < max_bytes
2278 ? bytes_remaining : max_bytes);
2279 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2280 (bytes_remaining == bytes
2281 ? PropModeReplace
2282 : PropModeAppend),
2283 data, chunk);
2284 data += chunk;
2285 bytes_remaining -= chunk;
2286 }
2287 UNBLOCK_INPUT;
2288 return string;
2289}
2290
2291
a87ed99c 2292DEFUN ("x-rotate-cut-buffers-internal", Fx_rotate_cut_buffers_internal,
8c1a1077
PJ
2293 Sx_rotate_cut_buffers_internal, 1, 1, 0,
2294 doc: /* Rotate the values of the cut buffers by the given number of step.
2295Positive means shift the values forward, negative means backward. */)
2296 (n)
ede4db72
RS
2297 Lisp_Object n;
2298{
3834c318
RS
2299 Window window;
2300 Atom props[8];
2301 Display *display;
378c33ca 2302 struct frame *sf = SELECTED_FRAME ();
ede4db72 2303
703e0710 2304 check_x ();
378c33ca 2305 display = FRAME_X_DISPLAY (sf);
3834c318 2306 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
b7826503 2307 CHECK_NUMBER (n);
3834c318
RS
2308 if (XINT (n) == 0)
2309 return n;
378c33ca 2310 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
04649dbc
KH
2311 {
2312 initialize_cut_buffers (display, window);
378c33ca 2313 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
04649dbc 2314 }
3834c318 2315
ede4db72
RS
2316 props[0] = XA_CUT_BUFFER0;
2317 props[1] = XA_CUT_BUFFER1;
2318 props[2] = XA_CUT_BUFFER2;
2319 props[3] = XA_CUT_BUFFER3;
2320 props[4] = XA_CUT_BUFFER4;
2321 props[5] = XA_CUT_BUFFER5;
2322 props[6] = XA_CUT_BUFFER6;
2323 props[7] = XA_CUT_BUFFER7;
2324 BLOCK_INPUT;
2325 XRotateWindowProperties (display, window, props, 8, XINT (n));
2326 UNBLOCK_INPUT;
2327 return n;
2328}
2329
2330#endif
2331\f
ede4db72
RS
2332void
2333syms_of_xselect ()
2334{
ede4db72
RS
2335 defsubr (&Sx_get_selection_internal);
2336 defsubr (&Sx_own_selection_internal);
2337 defsubr (&Sx_disown_selection_internal);
2338 defsubr (&Sx_selection_owner_p);
2339 defsubr (&Sx_selection_exists_p);
2340
2341#ifdef CUT_BUFFER_SUPPORT
a87ed99c
RS
2342 defsubr (&Sx_get_cut_buffer_internal);
2343 defsubr (&Sx_store_cut_buffer_internal);
2344 defsubr (&Sx_rotate_cut_buffers_internal);
ede4db72
RS
2345#endif
2346
2347 reading_selection_reply = Fcons (Qnil, Qnil);
2348 staticpro (&reading_selection_reply);
2349 reading_selection_window = 0;
2350 reading_which_selection = 0;
2351
2352 property_change_wait_list = 0;
2f65feb6 2353 prop_location_identifier = 0;
ede4db72
RS
2354 property_change_reply = Fcons (Qnil, Qnil);
2355 staticpro (&property_change_reply);
2356
2357 Vselection_alist = Qnil;
2358 staticpro (&Vselection_alist);
2359
2360 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
8c1a1077
PJ
2361 doc: /* An alist associating X Windows selection-types with functions.
2362These functions are called to convert the selection, with three args:
2363the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2364a desired type to which the selection should be converted;
2365and the local selection value (whatever was given to `x-own-selection').
2366
2367The function should return the value to send to the X server
2368\(typically a string). A return value of nil
2369means that the conversion could not be done.
2370A return value which is the symbol `NULL'
2371means that a side-effect was executed,
2372and there is no meaningful selection value. */);
ede4db72
RS
2373 Vselection_converter_alist = Qnil;
2374
2375 DEFVAR_LISP ("x-lost-selection-hooks", &Vx_lost_selection_hooks,
8c1a1077
PJ
2376 doc: /* A list of functions to be called when Emacs loses an X selection.
2377\(This happens when some other X client makes its own selection
2378or when a Lisp program explicitly clears the selection.)
2379The functions are called with one argument, the selection type
2380\(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
ede4db72
RS
2381 Vx_lost_selection_hooks = Qnil;
2382
2383 DEFVAR_LISP ("x-sent-selection-hooks", &Vx_sent_selection_hooks,
8c1a1077
PJ
2384 doc: /* A list of functions to be called when Emacs answers a selection request.
2385The functions are called with four arguments:
2386 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2387 - the selection-type which Emacs was asked to convert the
2388 selection into before sending (for example, `STRING' or `LENGTH');
2389 - a flag indicating success or failure for responding to the request.
2390We might have failed (and declined the request) for any number of reasons,
2391including being asked for a selection that we no longer own, or being asked
2392to convert into a type that we don't know about or that is inappropriate.
2393This hook doesn't let you change the behavior of Emacs's selection replies,
2394it merely informs you that they have happened. */);
ede4db72
RS
2395 Vx_sent_selection_hooks = Qnil;
2396
93e4ce1b 2397 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
8c1a1077
PJ
2398 doc: /* Coding system for communicating with other X clients.
2399When sending or receiving text via cut_buffer, selection, and clipboard,
2400the text is encoded or decoded by this coding system.
5a79ea57
EZ
2401The default value is `compound-text-with-extensions'. */);
2402 Vselection_coding_system = intern ("compound-text-with-extensions");
2584c9ec 2403
16cd5029 2404 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
8c1a1077
PJ
2405 doc: /* Coding system for the next communication with other X clients.
2406Usually, `selection-coding-system' is used for communicating with
f8f8d464
JB
2407other X clients. But, if this variable is set, it is used for the
2408next communication only. After the communication, this variable is
8c1a1077 2409set to nil. */);
16cd5029
KH
2410 Vnext_selection_coding_system = Qnil;
2411
ede4db72 2412 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
8c1a1077
PJ
2413 doc: /* Number of milliseconds to wait for a selection reply.
2414If the selection owner doesn't reply in this time, we give up.
2415A value of 0 means wait as long as necessary. This is initialized from the
2416\"*selectionTimeout\" resource. */);
ede4db72
RS
2417 x_selection_timeout = 0;
2418
2419 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
2420 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
2421 QSTRING = intern ("STRING"); staticpro (&QSTRING);
2422 QINTEGER = intern ("INTEGER"); staticpro (&QINTEGER);
2423 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
2424 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2425 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
e6c7c988 2426 QCOMPOUND_TEXT = intern ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
5109c8dd 2427 QUTF8_STRING = intern ("UTF8_STRING"); staticpro (&QUTF8_STRING);
ede4db72
RS
2428 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2429 QDELETE = intern ("DELETE"); staticpro (&QDELETE);
2430 QMULTIPLE = intern ("MULTIPLE"); staticpro (&QMULTIPLE);
2431 QINCR = intern ("INCR"); staticpro (&QINCR);
2432 QEMACS_TMP = intern ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
2433 QTARGETS = intern ("TARGETS"); staticpro (&QTARGETS);
2434 QATOM = intern ("ATOM"); staticpro (&QATOM);
2435 QATOM_PAIR = intern ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
2436 QNULL = intern ("NULL"); staticpro (&QNULL);
5a79ea57
EZ
2437 Qcompound_text_with_extensions = intern ("compound-text-with-extensions");
2438 staticpro (&Qcompound_text_with_extensions);
ede4db72
RS
2439
2440#ifdef CUT_BUFFER_SUPPORT
2441 QCUT_BUFFER0 = intern ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
2442 QCUT_BUFFER1 = intern ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
2443 QCUT_BUFFER2 = intern ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
2444 QCUT_BUFFER3 = intern ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
2445 QCUT_BUFFER4 = intern ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
2446 QCUT_BUFFER5 = intern ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
2447 QCUT_BUFFER6 = intern ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
2448 QCUT_BUFFER7 = intern ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
2449#endif
2450
2451}