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