* lisp/emacs-lisp/cl-generic.el (cl--generic-member-method): Fix paren typo.
[emacs.git] / src / xselect.c
blob027192dfea288889857394e30a531181c629f987
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2015 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Rewritten by jwz */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h> /* termhooks.h needs this */
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
30 #include <unistd.h>
32 #include "lisp.h"
33 #include "xterm.h" /* for all of the X includes */
34 #include "dispextern.h" /* frame.h seems to want this */
35 #include "frame.h" /* Need this to get the X window of selected_frame */
36 #include "blockinput.h"
37 #include "character.h"
38 #include "buffer.h"
39 #include "process.h"
40 #include "termhooks.h"
41 #include "keyboard.h"
43 #include <X11/Xproto.h>
45 struct prop_location;
46 struct selection_data;
48 static void x_decline_selection_request (struct input_event *);
49 static bool x_convert_selection (struct input_event *, Lisp_Object,
50 Lisp_Object, Atom, bool,
51 struct x_display_info *);
52 static bool waiting_for_other_props_on_window (Display *, Window);
53 static struct prop_location *expect_property_change (Display *, Window,
54 Atom, int);
55 static void unexpect_property_change (struct prop_location *);
56 static void wait_for_property_change (struct prop_location *);
57 static Lisp_Object x_get_window_property_as_lisp_data (struct x_display_info *,
58 Window, Atom,
59 Lisp_Object, Atom);
60 static Lisp_Object selection_data_to_lisp_data (struct x_display_info *,
61 const unsigned char *,
62 ptrdiff_t, Atom, int);
63 static void lisp_data_to_selection_data (struct x_display_info *, Lisp_Object,
64 struct selection_data *);
66 /* Printing traces to stderr. */
68 #ifdef TRACE_SELECTION
69 #define TRACE0(fmt) \
70 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid ())
71 #define TRACE1(fmt, a0) \
72 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0)
73 #define TRACE2(fmt, a0, a1) \
74 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1)
75 #define TRACE3(fmt, a0, a1, a2) \
76 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1, a2)
77 #else
78 #define TRACE0(fmt) (void) 0
79 #define TRACE1(fmt, a0) (void) 0
80 #define TRACE2(fmt, a0, a1) (void) 0
81 #endif
83 /* Bytes needed to represent 'long' data. This is as per libX11; it
84 is not necessarily sizeof (long). */
85 #define X_LONG_SIZE 4
87 /* If this is a smaller number than the max-request-size of the display,
88 emacs will use INCR selection transfer when the selection is larger
89 than this. The max-request-size is usually around 64k, so if you want
90 emacs to use incremental selection transfers when the selection is
91 smaller than that, set this. I added this mostly for debugging the
92 incremental transfer stuff, but it might improve server performance.
94 This value cannot exceed INT_MAX / max (X_LONG_SIZE, sizeof (long))
95 because it is multiplied by X_LONG_SIZE and by sizeof (long) in
96 subscript calculations. Similarly for PTRDIFF_MAX - 1 or SIZE_MAX
97 - 1 in place of INT_MAX. */
98 #define MAX_SELECTION_QUANTUM \
99 ((int) min (0xFFFFFF, (min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) - 1) \
100 / max (X_LONG_SIZE, sizeof (long)))))
102 static int
103 selection_quantum (Display *display)
105 long mrs = XMaxRequestSize (display);
106 return (mrs < MAX_SELECTION_QUANTUM / X_LONG_SIZE + 25
107 ? (mrs - 25) * X_LONG_SIZE
108 : MAX_SELECTION_QUANTUM);
111 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
112 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
115 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
116 handling. */
118 struct selection_event_queue
120 struct input_event event;
121 struct selection_event_queue *next;
124 static struct selection_event_queue *selection_queue;
126 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
128 static int x_queue_selection_requests;
130 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
132 static void
133 x_queue_event (struct input_event *event)
135 struct selection_event_queue *queue_tmp;
137 /* Don't queue repeated requests.
138 This only happens for large requests which uses the incremental protocol. */
139 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
141 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
143 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
144 x_decline_selection_request (event);
145 return;
149 queue_tmp = xmalloc (sizeof *queue_tmp);
150 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
151 queue_tmp->event = *event;
152 queue_tmp->next = selection_queue;
153 selection_queue = queue_tmp;
156 /* Start queuing SELECTION_REQUEST_EVENT events. */
158 static void
159 x_start_queuing_selection_requests (void)
161 if (x_queue_selection_requests)
162 emacs_abort ();
164 x_queue_selection_requests++;
165 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
168 /* Stop queuing SELECTION_REQUEST_EVENT events. */
170 static void
171 x_stop_queuing_selection_requests (void)
173 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
174 --x_queue_selection_requests;
176 /* Take all the queued events and put them back
177 so that they get processed afresh. */
179 while (selection_queue != NULL)
181 struct selection_event_queue *queue_tmp = selection_queue;
182 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
183 kbd_buffer_unget_event (&queue_tmp->event);
184 selection_queue = queue_tmp->next;
185 xfree (queue_tmp);
190 /* This converts a Lisp symbol to a server Atom, avoiding a server
191 roundtrip whenever possible. */
193 static Atom
194 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
196 Atom val;
197 if (NILP (sym)) return 0;
198 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
199 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
200 if (EQ (sym, QSTRING)) return XA_STRING;
201 if (EQ (sym, QINTEGER)) return XA_INTEGER;
202 if (EQ (sym, QATOM)) return XA_ATOM;
203 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
204 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
205 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
206 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
207 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
208 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
209 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
210 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
211 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
212 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
213 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
214 if (!SYMBOLP (sym)) emacs_abort ();
216 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
217 block_input ();
218 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
219 unblock_input ();
220 return val;
224 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
225 and calls to intern whenever possible. */
227 static Lisp_Object
228 x_atom_to_symbol (struct x_display_info *dpyinfo, Atom atom)
230 char *str;
231 Lisp_Object val;
233 if (! atom)
234 return Qnil;
236 switch (atom)
238 case XA_PRIMARY:
239 return QPRIMARY;
240 case XA_SECONDARY:
241 return QSECONDARY;
242 case XA_STRING:
243 return QSTRING;
244 case XA_INTEGER:
245 return QINTEGER;
246 case XA_ATOM:
247 return QATOM;
250 if (dpyinfo == NULL)
251 return Qnil;
252 if (atom == dpyinfo->Xatom_CLIPBOARD)
253 return QCLIPBOARD;
254 if (atom == dpyinfo->Xatom_TIMESTAMP)
255 return QTIMESTAMP;
256 if (atom == dpyinfo->Xatom_TEXT)
257 return QTEXT;
258 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
259 return QCOMPOUND_TEXT;
260 if (atom == dpyinfo->Xatom_UTF8_STRING)
261 return QUTF8_STRING;
262 if (atom == dpyinfo->Xatom_DELETE)
263 return QDELETE;
264 if (atom == dpyinfo->Xatom_MULTIPLE)
265 return QMULTIPLE;
266 if (atom == dpyinfo->Xatom_INCR)
267 return QINCR;
268 if (atom == dpyinfo->Xatom_EMACS_TMP)
269 return QEMACS_TMP;
270 if (atom == dpyinfo->Xatom_TARGETS)
271 return QTARGETS;
272 if (atom == dpyinfo->Xatom_NULL)
273 return QNULL;
275 block_input ();
276 str = XGetAtomName (dpyinfo->display, atom);
277 unblock_input ();
278 TRACE1 ("XGetAtomName --> %s", str);
279 if (! str) return Qnil;
280 val = intern (str);
281 block_input ();
282 /* This was allocated by Xlib, so use XFree. */
283 XFree (str);
284 unblock_input ();
285 return val;
288 /* Do protocol to assert ourself as a selection owner.
289 FRAME shall be the owner; it must be a valid X frame.
290 Update the Vselection_alist so that we can reply to later requests for
291 our selection. */
293 static void
294 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
295 Lisp_Object frame)
297 struct frame *f = XFRAME (frame);
298 Window selecting_window = FRAME_X_WINDOW (f);
299 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
300 Display *display = dpyinfo->display;
301 Time timestamp = dpyinfo->last_user_time;
302 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
304 block_input ();
305 x_catch_errors (display);
306 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
307 x_check_errors (display, "Can't set selection: %s");
308 x_uncatch_errors ();
309 unblock_input ();
311 /* Now update the local cache */
313 Lisp_Object selection_data;
314 Lisp_Object prev_value;
316 selection_data = list4 (selection_name, selection_value,
317 INTEGER_TO_CONS (timestamp), frame);
318 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
320 tset_selection_alist
321 (dpyinfo->terminal,
322 Fcons (selection_data, dpyinfo->terminal->Vselection_alist));
324 /* If we already owned the selection, remove the old selection
325 data. Don't use Fdelq as that may QUIT. */
326 if (!NILP (prev_value))
328 /* We know it's not the CAR, so it's easy. */
329 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
330 for (; CONSP (rest); rest = XCDR (rest))
331 if (EQ (prev_value, Fcar (XCDR (rest))))
333 XSETCDR (rest, XCDR (XCDR (rest)));
334 break;
340 /* Given a selection-name and desired type, look up our local copy of
341 the selection value and convert it to the type.
342 Return nil, a string, a vector, a symbol, an integer, or a cons
343 that CONS_TO_INTEGER could plausibly handle.
344 This function is used both for remote requests (LOCAL_REQUEST is zero)
345 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
347 This calls random Lisp code, and may signal or gc. */
349 static Lisp_Object
350 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
351 bool local_request, struct x_display_info *dpyinfo)
353 Lisp_Object local_value;
354 Lisp_Object handler_fn, value, check;
356 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
358 if (NILP (local_value)) return Qnil;
360 /* TIMESTAMP is a special case. */
361 if (EQ (target_type, QTIMESTAMP))
363 handler_fn = Qnil;
364 value = XCAR (XCDR (XCDR (local_value)));
366 else
368 /* Don't allow a quit within the converter.
369 When the user types C-g, he would be surprised
370 if by luck it came during a converter. */
371 ptrdiff_t count = SPECPDL_INDEX ();
372 specbind (Qinhibit_quit, Qt);
374 CHECK_SYMBOL (target_type);
375 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
376 /* gcpro is not needed here since nothing but HANDLER_FN
377 is live, and that ought to be a symbol. */
379 if (!NILP (handler_fn))
380 value = call3 (handler_fn,
381 selection_symbol, (local_request ? Qnil : target_type),
382 XCAR (XCDR (local_value)));
383 else
384 value = Qnil;
385 unbind_to (count, Qnil);
388 /* Make sure this value is of a type that we could transmit
389 to another X client. */
391 check = value;
392 if (CONSP (value)
393 && SYMBOLP (XCAR (value)))
394 check = XCDR (value);
396 if (STRINGP (check)
397 || VECTORP (check)
398 || SYMBOLP (check)
399 || INTEGERP (check)
400 || NILP (value))
401 return value;
402 /* Check for a value that CONS_TO_INTEGER could handle. */
403 else if (CONSP (check)
404 && INTEGERP (XCAR (check))
405 && (INTEGERP (XCDR (check))
407 (CONSP (XCDR (check))
408 && INTEGERP (XCAR (XCDR (check)))
409 && NILP (XCDR (XCDR (check))))))
410 return value;
412 signal_error ("Invalid data returned by selection-conversion function",
413 list2 (handler_fn, value));
416 /* Subroutines of x_reply_selection_request. */
418 /* Send a SelectionNotify event to the requestor with property=None,
419 meaning we were unable to do what they wanted. */
421 static void
422 x_decline_selection_request (struct input_event *event)
424 XEvent reply_base;
425 XSelectionEvent *reply = &(reply_base.xselection);
427 reply->type = SelectionNotify;
428 reply->display = SELECTION_EVENT_DISPLAY (event);
429 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
430 reply->selection = SELECTION_EVENT_SELECTION (event);
431 reply->time = SELECTION_EVENT_TIME (event);
432 reply->target = SELECTION_EVENT_TARGET (event);
433 reply->property = None;
435 /* The reason for the error may be that the receiver has
436 died in the meantime. Handle that case. */
437 block_input ();
438 x_catch_errors (reply->display);
439 XSendEvent (reply->display, reply->requestor, False, 0, &reply_base);
440 XFlush (reply->display);
441 x_uncatch_errors ();
442 unblock_input ();
445 /* This is the selection request currently being processed.
446 It is set to zero when the request is fully processed. */
447 static struct input_event *x_selection_current_request;
449 /* Display info in x_selection_request. */
451 static struct x_display_info *selection_request_dpyinfo;
453 /* Raw selection data, for sending to a requestor window. */
455 struct selection_data
457 unsigned char *data;
458 ptrdiff_t size;
459 int format;
460 Atom type;
461 bool nofree;
462 Atom property;
463 /* This can be set to non-NULL during x_reply_selection_request, if
464 the selection is waiting for an INCR transfer to complete. Don't
465 free these; that's done by unexpect_property_change. */
466 struct prop_location *wait_object;
467 struct selection_data *next;
470 /* Linked list of the above (in support of MULTIPLE targets). */
472 static struct selection_data *converted_selections;
474 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
475 static Atom conversion_fail_tag;
477 /* Used as an unwind-protect clause so that, if a selection-converter signals
478 an error, we tell the requestor that we were unable to do what they wanted
479 before we throw to top-level or go into the debugger or whatever. */
481 static void
482 x_selection_request_lisp_error (void)
484 struct selection_data *cs, *next;
486 for (cs = converted_selections; cs; cs = next)
488 next = cs->next;
489 if (! cs->nofree && cs->data)
490 xfree (cs->data);
491 xfree (cs);
493 converted_selections = NULL;
495 if (x_selection_current_request != 0
496 && selection_request_dpyinfo->display)
497 x_decline_selection_request (x_selection_current_request);
500 static void
501 x_catch_errors_unwind (void)
503 block_input ();
504 x_uncatch_errors ();
505 unblock_input ();
509 /* This stuff is so that INCR selections are reentrant (that is, so we can
510 be servicing multiple INCR selection requests simultaneously.) I haven't
511 actually tested that yet. */
513 /* Keep a list of the property changes that are awaited. */
515 struct prop_location
517 int identifier;
518 Display *display;
519 Window window;
520 Atom property;
521 int desired_state;
522 bool arrived;
523 struct prop_location *next;
526 static int prop_location_identifier;
528 static Lisp_Object property_change_reply;
530 static struct prop_location *property_change_reply_object;
532 static struct prop_location *property_change_wait_list;
535 /* Send the reply to a selection request event EVENT. */
537 #ifdef TRACE_SELECTION
538 static int x_reply_selection_request_cnt;
539 #endif /* TRACE_SELECTION */
541 static void
542 x_reply_selection_request (struct input_event *event,
543 struct x_display_info *dpyinfo)
545 XEvent reply_base;
546 XSelectionEvent *reply = &(reply_base.xselection);
547 Display *display = SELECTION_EVENT_DISPLAY (event);
548 Window window = SELECTION_EVENT_REQUESTOR (event);
549 ptrdiff_t bytes_remaining;
550 int max_bytes = selection_quantum (display);
551 ptrdiff_t count = SPECPDL_INDEX ();
552 struct selection_data *cs;
554 reply->type = SelectionNotify;
555 reply->display = display;
556 reply->requestor = window;
557 reply->selection = SELECTION_EVENT_SELECTION (event);
558 reply->time = SELECTION_EVENT_TIME (event);
559 reply->target = SELECTION_EVENT_TARGET (event);
560 reply->property = SELECTION_EVENT_PROPERTY (event);
561 if (reply->property == None)
562 reply->property = reply->target;
564 block_input ();
565 /* The protected block contains wait_for_property_change, which can
566 run random lisp code (process handlers) or signal. Therefore, we
567 put the x_uncatch_errors call in an unwind. */
568 record_unwind_protect_void (x_catch_errors_unwind);
569 x_catch_errors (display);
571 /* Loop over converted selections, storing them in the requested
572 properties. If data is large, only store the first N bytes
573 (section 2.7.2 of ICCCM). Note that we store the data for a
574 MULTIPLE request in the opposite order; the ICCM says only that
575 the conversion itself must be done in the same order. */
576 for (cs = converted_selections; cs; cs = cs->next)
578 if (cs->property == None)
579 continue;
581 bytes_remaining = cs->size;
582 bytes_remaining *= cs->format >> 3;
583 if (bytes_remaining <= max_bytes)
585 /* Send all the data at once, with minimal handshaking. */
586 TRACE1 ("Sending all %"pD"d bytes", bytes_remaining);
587 XChangeProperty (display, window, cs->property,
588 cs->type, cs->format, PropModeReplace,
589 cs->data, cs->size);
591 else
593 /* Send an INCR tag to initiate incremental transfer. */
594 long value[1];
596 TRACE2 ("Start sending %"pD"d bytes incrementally (%s)",
597 bytes_remaining, XGetAtomName (display, cs->property));
598 cs->wait_object
599 = expect_property_change (display, window, cs->property,
600 PropertyDelete);
602 /* XChangeProperty expects an array of long even if long is
603 more than 32 bits. */
604 value[0] = min (bytes_remaining, X_LONG_MAX);
605 XChangeProperty (display, window, cs->property,
606 dpyinfo->Xatom_INCR, 32, PropModeReplace,
607 (unsigned char *) value, 1);
608 XSelectInput (display, window, PropertyChangeMask);
612 /* Now issue the SelectionNotify event. */
613 XSendEvent (display, window, False, 0, &reply_base);
614 XFlush (display);
616 #ifdef TRACE_SELECTION
618 char *sel = XGetAtomName (display, reply->selection);
619 char *tgt = XGetAtomName (display, reply->target);
620 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
621 sel, tgt, ++x_reply_selection_request_cnt);
622 if (sel) XFree (sel);
623 if (tgt) XFree (tgt);
625 #endif /* TRACE_SELECTION */
627 /* Finish sending the rest of each of the INCR values. This should
628 be improved; there's a chance of deadlock if more than one
629 subtarget in a MULTIPLE selection requires an INCR transfer, and
630 the requestor and Emacs loop waiting on different transfers. */
631 for (cs = converted_selections; cs; cs = cs->next)
632 if (cs->wait_object)
634 int format_bytes = cs->format / 8;
635 bool had_errors_p = x_had_errors_p (display);
636 unblock_input ();
638 bytes_remaining = cs->size;
639 bytes_remaining *= format_bytes;
641 /* Wait for the requestor to ack by deleting the property.
642 This can run Lisp code (process handlers) or signal. */
643 if (! had_errors_p)
645 TRACE1 ("Waiting for ACK (deletion of %s)",
646 XGetAtomName (display, cs->property));
647 wait_for_property_change (cs->wait_object);
649 else
650 unexpect_property_change (cs->wait_object);
652 while (bytes_remaining)
654 int i = ((bytes_remaining < max_bytes)
655 ? bytes_remaining
656 : max_bytes) / format_bytes;
657 block_input ();
659 cs->wait_object
660 = expect_property_change (display, window, cs->property,
661 PropertyDelete);
663 TRACE1 ("Sending increment of %d elements", i);
664 TRACE1 ("Set %s to increment data",
665 XGetAtomName (display, cs->property));
667 /* Append the next chunk of data to the property. */
668 XChangeProperty (display, window, cs->property,
669 cs->type, cs->format, PropModeAppend,
670 cs->data, i);
671 bytes_remaining -= i * format_bytes;
672 cs->data += i * ((cs->format == 32) ? sizeof (long)
673 : format_bytes);
674 XFlush (display);
675 had_errors_p = x_had_errors_p (display);
676 unblock_input ();
678 if (had_errors_p) break;
680 /* Wait for the requestor to ack this chunk by deleting
681 the property. This can run Lisp code or signal. */
682 TRACE1 ("Waiting for increment ACK (deletion of %s)",
683 XGetAtomName (display, cs->property));
684 wait_for_property_change (cs->wait_object);
687 /* Now write a zero-length chunk to the property to tell the
688 requestor that we're done. */
689 block_input ();
690 if (! waiting_for_other_props_on_window (display, window))
691 XSelectInput (display, window, 0);
693 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
694 XGetAtomName (display, cs->property));
695 XChangeProperty (display, window, cs->property,
696 cs->type, cs->format, PropModeReplace,
697 cs->data, 0);
698 TRACE0 ("Done sending incrementally");
701 /* rms, 2003-01-03: I think I have fixed this bug. */
702 /* The window we're communicating with may have been deleted
703 in the meantime (that's a real situation from a bug report).
704 In this case, there may be events in the event queue still
705 referring to the deleted window, and we'll get a BadWindow error
706 in XTread_socket when processing the events. I don't have
707 an idea how to fix that. gerd, 2001-01-98. */
708 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
709 delivered before uncatch errors. */
710 XSync (display, False);
711 unblock_input ();
713 /* GTK queues events in addition to the queue in Xlib. So we
714 UNBLOCK to enter the event loop and get possible errors delivered,
715 and then BLOCK again because x_uncatch_errors requires it. */
716 block_input ();
717 /* This calls x_uncatch_errors. */
718 unbind_to (count, Qnil);
719 unblock_input ();
722 /* Handle a SelectionRequest event EVENT.
723 This is called from keyboard.c when such an event is found in the queue. */
725 static void
726 x_handle_selection_request (struct input_event *event)
728 struct gcpro gcpro1, gcpro2;
729 Time local_selection_time;
731 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
732 Atom selection = SELECTION_EVENT_SELECTION (event);
733 Lisp_Object selection_symbol = x_atom_to_symbol (dpyinfo, selection);
734 Atom target = SELECTION_EVENT_TARGET (event);
735 Lisp_Object target_symbol = x_atom_to_symbol (dpyinfo, target);
736 Atom property = SELECTION_EVENT_PROPERTY (event);
737 Lisp_Object local_selection_data;
738 bool success = false;
739 ptrdiff_t count = SPECPDL_INDEX ();
740 GCPRO2 (local_selection_data, target_symbol);
742 if (!dpyinfo) goto DONE;
744 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
746 /* Decline if we don't own any selections. */
747 if (NILP (local_selection_data)) goto DONE;
749 /* Decline requests issued prior to our acquiring the selection. */
750 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
751 Time, local_selection_time);
752 if (SELECTION_EVENT_TIME (event) != CurrentTime
753 && local_selection_time > SELECTION_EVENT_TIME (event))
754 goto DONE;
756 x_selection_current_request = event;
757 selection_request_dpyinfo = dpyinfo;
758 record_unwind_protect_void (x_selection_request_lisp_error);
760 /* We might be able to handle nested x_handle_selection_requests,
761 but this is difficult to test, and seems unimportant. */
762 x_start_queuing_selection_requests ();
763 record_unwind_protect_void (x_stop_queuing_selection_requests);
765 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
766 SDATA (SYMBOL_NAME (selection_symbol)),
767 SDATA (SYMBOL_NAME (target_symbol)));
769 if (EQ (target_symbol, QMULTIPLE))
771 /* For MULTIPLE targets, the event property names a list of atom
772 pairs; the first atom names a target and the second names a
773 non-None property. */
774 Window requestor = SELECTION_EVENT_REQUESTOR (event);
775 Lisp_Object multprop;
776 ptrdiff_t j, nselections;
778 if (property == None) goto DONE;
779 multprop
780 = x_get_window_property_as_lisp_data (dpyinfo, requestor, property,
781 QMULTIPLE, selection);
783 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
784 goto DONE;
786 nselections = ASIZE (multprop) / 2;
787 /* Perform conversions. This can signal. */
788 for (j = 0; j < nselections; j++)
790 Lisp_Object subtarget = AREF (multprop, 2*j);
791 Atom subproperty = symbol_to_x_atom (dpyinfo,
792 AREF (multprop, 2*j+1));
794 if (subproperty != None)
795 x_convert_selection (event, selection_symbol, subtarget,
796 subproperty, true, dpyinfo);
798 success = true;
800 else
802 if (property == None)
803 property = SELECTION_EVENT_TARGET (event);
804 success = x_convert_selection (event, selection_symbol,
805 target_symbol, property,
806 false, dpyinfo);
809 DONE:
811 if (success)
812 x_reply_selection_request (event, dpyinfo);
813 else
814 x_decline_selection_request (event);
815 x_selection_current_request = 0;
817 /* Run the `x-sent-selection-functions' abnormal hook. */
818 if (!NILP (Vx_sent_selection_functions)
819 && !EQ (Vx_sent_selection_functions, Qunbound))
820 CALLN (Frun_hook_with_args, Qx_sent_selection_functions,
821 selection_symbol, target_symbol, success ? Qt : Qnil);
823 unbind_to (count, Qnil);
824 UNGCPRO;
827 /* Perform the requested selection conversion, and write the data to
828 the converted_selections linked list, where it can be accessed by
829 x_reply_selection_request. If FOR_MULTIPLE, write out
830 the data even if conversion fails, using conversion_fail_tag.
832 Return true iff successful. */
834 static bool
835 x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
836 Lisp_Object target_symbol, Atom property,
837 bool for_multiple, struct x_display_info *dpyinfo)
839 struct gcpro gcpro1;
840 Lisp_Object lisp_selection;
841 struct selection_data *cs;
842 GCPRO1 (lisp_selection);
844 lisp_selection
845 = x_get_local_selection (selection_symbol, target_symbol,
846 false, dpyinfo);
848 /* A nil return value means we can't perform the conversion. */
849 if (NILP (lisp_selection)
850 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
852 if (for_multiple)
854 cs = xmalloc (sizeof *cs);
855 cs->data = (unsigned char *) &conversion_fail_tag;
856 cs->size = 1;
857 cs->format = 32;
858 cs->type = XA_ATOM;
859 cs->nofree = true;
860 cs->property = property;
861 cs->wait_object = NULL;
862 cs->next = converted_selections;
863 converted_selections = cs;
866 UNGCPRO;
867 return false;
870 /* Otherwise, record the converted selection to binary. */
871 cs = xmalloc (sizeof *cs);
872 cs->data = NULL;
873 cs->nofree = true;
874 cs->property = property;
875 cs->wait_object = NULL;
876 cs->next = converted_selections;
877 converted_selections = cs;
878 lisp_data_to_selection_data (dpyinfo, lisp_selection, cs);
879 UNGCPRO;
880 return true;
883 /* Handle a SelectionClear event EVENT, which indicates that some
884 client cleared out our previously asserted selection.
885 This is called from keyboard.c when such an event is found in the queue. */
887 static void
888 x_handle_selection_clear (struct input_event *event)
890 Atom selection = SELECTION_EVENT_SELECTION (event);
891 Time changed_owner_time = SELECTION_EVENT_TIME (event);
893 Lisp_Object selection_symbol, local_selection_data;
894 Time local_selection_time;
895 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
896 Lisp_Object Vselection_alist;
898 TRACE0 ("x_handle_selection_clear");
900 if (!dpyinfo) return;
902 selection_symbol = x_atom_to_symbol (dpyinfo, selection);
903 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
905 /* Well, we already believe that we don't own it, so that's just fine. */
906 if (NILP (local_selection_data)) return;
908 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
909 Time, local_selection_time);
911 /* We have reasserted the selection since this SelectionClear was
912 generated, so we can disregard it. */
913 if (changed_owner_time != CurrentTime
914 && local_selection_time > changed_owner_time)
915 return;
917 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
918 Vselection_alist = dpyinfo->terminal->Vselection_alist;
919 if (EQ (local_selection_data, CAR (Vselection_alist)))
920 Vselection_alist = XCDR (Vselection_alist);
921 else
923 Lisp_Object rest;
924 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
925 if (EQ (local_selection_data, CAR (XCDR (rest))))
927 XSETCDR (rest, XCDR (XCDR (rest)));
928 break;
931 tset_selection_alist (dpyinfo->terminal, Vselection_alist);
933 /* Run the `x-lost-selection-functions' abnormal hook. */
934 CALLN (Frun_hook_with_args, Qx_lost_selection_functions, selection_symbol);
936 redisplay_preserve_echo_area (20);
939 void
940 x_handle_selection_event (struct input_event *event)
942 TRACE0 ("x_handle_selection_event");
943 if (event->kind != SELECTION_REQUEST_EVENT)
944 x_handle_selection_clear (event);
945 else if (x_queue_selection_requests)
946 x_queue_event (event);
947 else
948 x_handle_selection_request (event);
952 /* Clear all selections that were made from frame F.
953 We do this when about to delete a frame. */
955 void
956 x_clear_frame_selections (struct frame *f)
958 Lisp_Object frame;
959 Lisp_Object rest;
960 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
961 struct terminal *t = dpyinfo->terminal;
963 XSETFRAME (frame, f);
965 /* Delete elements from the beginning of Vselection_alist. */
966 while (CONSP (t->Vselection_alist)
967 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
969 /* Run the `x-lost-selection-functions' abnormal hook. */
970 CALLN (Frun_hook_with_args, Qx_lost_selection_functions,
971 Fcar (Fcar (t->Vselection_alist)));
973 tset_selection_alist (t, XCDR (t->Vselection_alist));
976 /* Delete elements after the beginning of Vselection_alist. */
977 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
978 if (CONSP (XCDR (rest))
979 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
981 CALLN (Frun_hook_with_args, Qx_lost_selection_functions,
982 XCAR (XCAR (XCDR (rest))));
983 XSETCDR (rest, XCDR (XCDR (rest)));
984 break;
988 /* True if any properties for DISPLAY and WINDOW
989 are on the list of what we are waiting for. */
991 static bool
992 waiting_for_other_props_on_window (Display *display, Window window)
994 for (struct prop_location *p = property_change_wait_list; p; p = p->next)
995 if (p->display == display && p->window == window)
996 return true;
997 return false;
1000 /* Add an entry to the list of property changes we are waiting for.
1001 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1002 The return value is a number that uniquely identifies
1003 this awaited property change. */
1005 static struct prop_location *
1006 expect_property_change (Display *display, Window window,
1007 Atom property, int state)
1009 struct prop_location *pl = xmalloc (sizeof *pl);
1010 pl->identifier = ++prop_location_identifier;
1011 pl->display = display;
1012 pl->window = window;
1013 pl->property = property;
1014 pl->desired_state = state;
1015 pl->next = property_change_wait_list;
1016 pl->arrived = false;
1017 property_change_wait_list = pl;
1018 return pl;
1021 /* Delete an entry from the list of property changes we are waiting for.
1022 IDENTIFIER is the number that uniquely identifies the entry. */
1024 static void
1025 unexpect_property_change (struct prop_location *location)
1027 struct prop_location *prop, **pprev = &property_change_wait_list;
1029 for (prop = property_change_wait_list; prop; prop = *pprev)
1031 if (prop == location)
1033 *pprev = prop->next;
1034 xfree (prop);
1035 break;
1037 else
1038 pprev = &prop->next;
1042 /* Remove the property change expectation element for IDENTIFIER. */
1044 static void
1045 wait_for_property_change_unwind (void *loc)
1047 struct prop_location *location = loc;
1049 unexpect_property_change (location);
1050 if (location == property_change_reply_object)
1051 property_change_reply_object = 0;
1054 /* Actually wait for a property change.
1055 IDENTIFIER should be the value that expect_property_change returned. */
1057 static void
1058 wait_for_property_change (struct prop_location *location)
1060 ptrdiff_t count = SPECPDL_INDEX ();
1062 if (property_change_reply_object)
1063 emacs_abort ();
1065 /* Make sure to do unexpect_property_change if we quit or err. */
1066 record_unwind_protect_ptr (wait_for_property_change_unwind, location);
1068 XSETCAR (property_change_reply, Qnil);
1069 property_change_reply_object = location;
1071 /* If the event we are waiting for arrives beyond here, it will set
1072 property_change_reply, because property_change_reply_object says so. */
1073 if (! location->arrived)
1075 EMACS_INT timeout = max (0, x_selection_timeout);
1076 EMACS_INT secs = timeout / 1000;
1077 int nsecs = (timeout % 1000) * 1000000;
1078 TRACE2 (" Waiting %"pI"d secs, %d nsecs", secs, nsecs);
1079 wait_reading_process_output (secs, nsecs, 0, false,
1080 property_change_reply, NULL, 0);
1082 if (NILP (XCAR (property_change_reply)))
1084 TRACE0 (" Timed out");
1085 error ("Timed out waiting for property-notify event");
1089 unbind_to (count, Qnil);
1092 /* Called from XTread_socket in response to a PropertyNotify event. */
1094 void
1095 x_handle_property_notify (const XPropertyEvent *event)
1097 struct prop_location *rest;
1099 for (rest = property_change_wait_list; rest; rest = rest->next)
1101 if (!rest->arrived
1102 && rest->property == event->atom
1103 && rest->window == event->window
1104 && rest->display == event->display
1105 && rest->desired_state == event->state)
1107 TRACE2 ("Expected %s of property %s",
1108 (event->state == PropertyDelete ? "deletion" : "change"),
1109 XGetAtomName (event->display, event->atom));
1111 rest->arrived = true;
1113 /* If this is the one wait_for_property_change is waiting for,
1114 tell it to wake up. */
1115 if (rest == property_change_reply_object)
1116 XSETCAR (property_change_reply, Qt);
1118 return;
1125 /* Variables for communication with x_handle_selection_notify. */
1126 static Atom reading_which_selection;
1127 static Lisp_Object reading_selection_reply;
1128 static Window reading_selection_window;
1130 /* Do protocol to read selection-data from the server.
1131 Converts this to Lisp data and returns it.
1132 FRAME is the frame whose X window shall request the selection. */
1134 static Lisp_Object
1135 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1136 Lisp_Object time_stamp, Lisp_Object frame)
1138 struct frame *f = XFRAME (frame);
1139 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1140 Display *display = dpyinfo->display;
1141 Window requestor_window = FRAME_X_WINDOW (f);
1142 Time requestor_time = dpyinfo->last_user_time;
1143 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1144 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1145 Atom type_atom = (CONSP (target_type)
1146 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1147 : symbol_to_x_atom (dpyinfo, target_type));
1148 EMACS_INT timeout, secs;
1149 int nsecs;
1151 if (!FRAME_LIVE_P (f))
1152 return Qnil;
1154 if (! NILP (time_stamp))
1155 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1157 block_input ();
1158 TRACE2 ("Get selection %s, type %s",
1159 XGetAtomName (display, type_atom),
1160 XGetAtomName (display, target_property));
1162 x_catch_errors (display);
1163 XConvertSelection (display, selection_atom, type_atom, target_property,
1164 requestor_window, requestor_time);
1165 x_check_errors (display, "Can't convert selection: %s");
1166 x_uncatch_errors ();
1168 /* Prepare to block until the reply has been read. */
1169 reading_selection_window = requestor_window;
1170 reading_which_selection = selection_atom;
1171 XSETCAR (reading_selection_reply, Qnil);
1173 /* It should not be necessary to stop handling selection requests
1174 during this time. In fact, the SAVE_TARGETS mechanism requires
1175 us to handle a clipboard manager's requests before it returns
1176 SelectionNotify. */
1177 #if false
1178 x_start_queuing_selection_requests ();
1179 record_unwind_protect_void (x_stop_queuing_selection_requests);
1180 #endif
1182 unblock_input ();
1184 /* This allows quits. Also, don't wait forever. */
1185 timeout = max (0, x_selection_timeout);
1186 secs = timeout / 1000;
1187 nsecs = (timeout % 1000) * 1000000;
1188 TRACE1 (" Start waiting %"pI"d secs for SelectionNotify", secs);
1189 wait_reading_process_output (secs, nsecs, 0, false,
1190 reading_selection_reply, NULL, 0);
1191 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1193 if (NILP (XCAR (reading_selection_reply)))
1194 error ("Timed out waiting for reply from selection owner");
1195 if (EQ (XCAR (reading_selection_reply), Qlambda))
1196 return Qnil;
1198 /* Otherwise, the selection is waiting for us on the requested property. */
1199 return
1200 x_get_window_property_as_lisp_data (dpyinfo, requestor_window,
1201 target_property, target_type,
1202 selection_atom);
1205 /* Subroutines of x_get_window_property_as_lisp_data */
1207 /* Use xfree, not XFree, to free the data obtained with this function. */
1209 static void
1210 x_get_window_property (Display *display, Window window, Atom property,
1211 unsigned char **data_ret, ptrdiff_t *bytes_ret,
1212 Atom *actual_type_ret, int *actual_format_ret,
1213 unsigned long *actual_size_ret)
1215 ptrdiff_t total_size;
1216 unsigned long bytes_remaining;
1217 ptrdiff_t offset = 0;
1218 unsigned char *data = 0;
1219 unsigned char *tmp_data = 0;
1220 int result;
1221 int buffer_size = selection_quantum (display);
1223 /* Wide enough to avoid overflow in expressions using it. */
1224 ptrdiff_t x_long_size = X_LONG_SIZE;
1226 /* Maximum value for TOTAL_SIZE. It cannot exceed PTRDIFF_MAX - 1
1227 and SIZE_MAX - 1, for an extra byte at the end. And it cannot
1228 exceed LONG_MAX * X_LONG_SIZE, for XGetWindowProperty. */
1229 ptrdiff_t total_size_max =
1230 ((min (PTRDIFF_MAX, SIZE_MAX) - 1) / x_long_size < LONG_MAX
1231 ? min (PTRDIFF_MAX, SIZE_MAX) - 1
1232 : LONG_MAX * x_long_size);
1234 block_input ();
1236 /* First probe the thing to find out how big it is. */
1237 result = XGetWindowProperty (display, window, property,
1238 0, 0, False, AnyPropertyType,
1239 actual_type_ret, actual_format_ret,
1240 actual_size_ret,
1241 &bytes_remaining, &tmp_data);
1242 if (result != Success)
1243 goto done;
1245 /* This was allocated by Xlib, so use XFree. */
1246 XFree (tmp_data);
1248 if (*actual_type_ret == None || *actual_format_ret == 0)
1249 goto done;
1251 if (total_size_max < bytes_remaining)
1252 goto size_overflow;
1253 total_size = bytes_remaining;
1254 data = xmalloc (total_size + 1);
1256 /* Now read, until we've gotten it all. */
1257 while (bytes_remaining)
1259 ptrdiff_t bytes_gotten;
1260 int bytes_per_item;
1261 result
1262 = XGetWindowProperty (display, window, property,
1263 offset / X_LONG_SIZE,
1264 buffer_size / X_LONG_SIZE,
1265 False,
1266 AnyPropertyType,
1267 actual_type_ret, actual_format_ret,
1268 actual_size_ret, &bytes_remaining, &tmp_data);
1270 /* If this doesn't return Success at this point, it means that
1271 some clod deleted the selection while we were in the midst of
1272 reading it. Deal with that, I guess.... */
1273 if (result != Success)
1274 break;
1276 bytes_per_item = *actual_format_ret >> 3;
1277 eassert (*actual_size_ret <= buffer_size / bytes_per_item);
1279 /* The man page for XGetWindowProperty says:
1280 "If the returned format is 32, the returned data is represented
1281 as a long array and should be cast to that type to obtain the
1282 elements."
1283 This applies even if long is more than 32 bits, the X library
1284 converts from 32 bit elements received from the X server to long
1285 and passes the long array to us. Thus, for that case memcpy can not
1286 be used. We convert to a 32 bit type here, because so much code
1287 assume on that.
1289 The bytes and offsets passed to XGetWindowProperty refers to the
1290 property and those are indeed in 32 bit quantities if format is 32. */
1292 bytes_gotten = *actual_size_ret;
1293 bytes_gotten *= bytes_per_item;
1295 TRACE2 ("Read %"pD"d bytes from property %s",
1296 bytes_gotten, XGetAtomName (display, property));
1298 if (total_size - offset < bytes_gotten)
1300 unsigned char *data1;
1301 ptrdiff_t remaining_lim = total_size_max - offset - bytes_gotten;
1302 if (remaining_lim < 0 || remaining_lim < bytes_remaining)
1303 goto size_overflow;
1304 total_size = offset + bytes_gotten + bytes_remaining;
1305 data1 = xrealloc (data, total_size + 1);
1306 data = data1;
1309 if (BITS_PER_LONG > 32 && *actual_format_ret == 32)
1311 unsigned long i;
1312 int *idata = (int *) (data + offset);
1313 long *ldata = (long *) tmp_data;
1315 for (i = 0; i < *actual_size_ret; ++i)
1316 idata[i] = ldata[i];
1318 else
1319 memcpy (data + offset, tmp_data, bytes_gotten);
1321 offset += bytes_gotten;
1323 /* This was allocated by Xlib, so use XFree. */
1324 XFree (tmp_data);
1327 XFlush (display);
1328 data[offset] = '\0';
1330 done:
1331 unblock_input ();
1332 *data_ret = data;
1333 *bytes_ret = offset;
1334 return;
1336 size_overflow:
1337 if (data)
1338 xfree (data);
1339 unblock_input ();
1340 memory_full (SIZE_MAX);
1343 /* Use xfree, not XFree, to free the data obtained with this function. */
1345 static void
1346 receive_incremental_selection (struct x_display_info *dpyinfo,
1347 Window window, Atom property,
1348 Lisp_Object target_type,
1349 unsigned int min_size_bytes,
1350 unsigned char **data_ret,
1351 ptrdiff_t *size_bytes_ret,
1352 Atom *type_ret, int *format_ret,
1353 unsigned long *size_ret)
1355 ptrdiff_t offset = 0;
1356 struct prop_location *wait_object;
1357 Display *display = dpyinfo->display;
1359 if (min (PTRDIFF_MAX, SIZE_MAX) < min_size_bytes)
1360 memory_full (SIZE_MAX);
1361 *data_ret = xmalloc (min_size_bytes);
1362 *size_bytes_ret = min_size_bytes;
1364 TRACE1 ("Read %u bytes incrementally", min_size_bytes);
1366 /* At this point, we have read an INCR property.
1367 Delete the property to ack it.
1368 (But first, prepare to receive the next event in this handshake.)
1370 Now, we must loop, waiting for the sending window to put a value on
1371 that property, then reading the property, then deleting it to ack.
1372 We are done when the sender places a property of length 0.
1374 block_input ();
1375 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1376 TRACE1 (" Delete property %s",
1377 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1378 XDeleteProperty (display, window, property);
1379 TRACE1 (" Expect new value of property %s",
1380 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1381 wait_object = expect_property_change (display, window, property,
1382 PropertyNewValue);
1383 XFlush (display);
1384 unblock_input ();
1386 while (true)
1388 unsigned char *tmp_data;
1389 ptrdiff_t tmp_size_bytes;
1391 TRACE0 (" Wait for property change");
1392 wait_for_property_change (wait_object);
1394 /* expect it again immediately, because x_get_window_property may
1395 .. no it won't, I don't get it.
1396 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1397 TRACE0 (" Get property value");
1398 x_get_window_property (display, window, property,
1399 &tmp_data, &tmp_size_bytes,
1400 type_ret, format_ret, size_ret);
1402 TRACE1 (" Read increment of %"pD"d bytes", tmp_size_bytes);
1404 if (tmp_size_bytes == 0) /* we're done */
1406 TRACE0 ("Done reading incrementally");
1408 if (! waiting_for_other_props_on_window (display, window))
1409 XSelectInput (display, window, STANDARD_EVENT_SET);
1410 /* Use xfree, not XFree, because x_get_window_property
1411 calls xmalloc itself. */
1412 xfree (tmp_data);
1413 break;
1416 block_input ();
1417 TRACE1 (" ACK by deleting property %s",
1418 XGetAtomName (display, property));
1419 XDeleteProperty (display, window, property);
1420 wait_object = expect_property_change (display, window, property,
1421 PropertyNewValue);
1422 XFlush (display);
1423 unblock_input ();
1425 if (*size_bytes_ret - offset < tmp_size_bytes)
1426 *data_ret = xpalloc (*data_ret, size_bytes_ret,
1427 tmp_size_bytes - (*size_bytes_ret - offset),
1428 -1, 1);
1430 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1431 offset += tmp_size_bytes;
1433 /* Use xfree, not XFree, because x_get_window_property
1434 calls xmalloc itself. */
1435 xfree (tmp_data);
1440 /* Fetch a value from property PROPERTY of X window WINDOW on display
1441 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1442 if this fails. */
1444 static Lisp_Object
1445 x_get_window_property_as_lisp_data (struct x_display_info *dpyinfo,
1446 Window window, Atom property,
1447 Lisp_Object target_type,
1448 Atom selection_atom)
1450 Atom actual_type;
1451 int actual_format;
1452 unsigned long actual_size;
1453 unsigned char *data = 0;
1454 ptrdiff_t bytes = 0;
1455 Lisp_Object val;
1456 Display *display = dpyinfo->display;
1458 TRACE0 ("Reading selection data");
1460 x_get_window_property (display, window, property, &data, &bytes,
1461 &actual_type, &actual_format, &actual_size);
1462 if (! data)
1464 block_input ();
1465 bool there_is_a_selection_owner
1466 = XGetSelectionOwner (display, selection_atom) != 0;
1467 unblock_input ();
1468 if (there_is_a_selection_owner)
1469 signal_error ("Selection owner couldn't convert",
1470 actual_type
1471 ? list2 (target_type,
1472 x_atom_to_symbol (dpyinfo, actual_type))
1473 : target_type);
1474 else
1475 signal_error ("No selection",
1476 x_atom_to_symbol (dpyinfo, selection_atom));
1479 if (actual_type == dpyinfo->Xatom_INCR)
1481 /* That wasn't really the data, just the beginning. */
1483 unsigned int min_size_bytes = * ((unsigned int *) data);
1484 block_input ();
1485 /* Use xfree, not XFree, because x_get_window_property
1486 calls xmalloc itself. */
1487 xfree (data);
1488 unblock_input ();
1489 receive_incremental_selection (dpyinfo, window, property, target_type,
1490 min_size_bytes, &data, &bytes,
1491 &actual_type, &actual_format,
1492 &actual_size);
1495 block_input ();
1496 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1497 XDeleteProperty (display, window, property);
1498 XFlush (display);
1499 unblock_input ();
1501 /* It's been read. Now convert it to a lisp object in some semi-rational
1502 manner. */
1503 val = selection_data_to_lisp_data (dpyinfo, data, bytes,
1504 actual_type, actual_format);
1506 /* Use xfree, not XFree, because x_get_window_property
1507 calls xmalloc itself. */
1508 xfree (data);
1509 return val;
1512 /* These functions convert from the selection data read from the server into
1513 something that we can use from Lisp, and vice versa.
1515 Type: Format: Size: Lisp Type:
1516 ----- ------- ----- -----------
1517 * 8 * String
1518 ATOM 32 1 Symbol
1519 ATOM 32 > 1 Vector of Symbols
1520 * 16 1 Integer
1521 * 16 > 1 Vector of Integers
1522 * 32 1 if <=16 bits: Integer
1523 if > 16 bits: Cons of top16, bot16
1524 * 32 > 1 Vector of the above
1526 When converting a Lisp number to C, it is assumed to be of format 16 if
1527 it is an integer, and of format 32 if it is a cons of two integers.
1529 When converting a vector of numbers from Lisp to C, it is assumed to be
1530 of format 16 if every element in the vector is an integer, and is assumed
1531 to be of format 32 if any element is a cons of two integers.
1533 When converting an object to C, it may be of the form (SYMBOL . <data>)
1534 where SYMBOL is what we should claim that the type is. Format and
1535 representation are as above.
1537 Important: When format is 32, data should contain an array of int,
1538 not an array of long as the X library returns. This makes a difference
1539 when sizeof(long) != sizeof(int). */
1543 static Lisp_Object
1544 selection_data_to_lisp_data (struct x_display_info *dpyinfo,
1545 const unsigned char *data,
1546 ptrdiff_t size, Atom type, int format)
1548 if (type == dpyinfo->Xatom_NULL)
1549 return QNULL;
1551 /* Convert any 8-bit data to a string, for compactness. */
1552 else if (format == 8)
1554 Lisp_Object str, lispy_type;
1556 str = make_unibyte_string ((char *) data, size);
1557 /* Indicate that this string is from foreign selection by a text
1558 property `foreign-selection' so that the caller of
1559 x-get-selection-internal (usually x-get-selection) can know
1560 that the string must be decode. */
1561 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1562 lispy_type = QCOMPOUND_TEXT;
1563 else if (type == dpyinfo->Xatom_UTF8_STRING)
1564 lispy_type = QUTF8_STRING;
1565 else
1566 lispy_type = QSTRING;
1567 Fput_text_property (make_number (0), make_number (size),
1568 Qforeign_selection, lispy_type, str);
1569 return str;
1571 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1572 a vector of symbols. */
1573 else if (type == XA_ATOM
1574 /* Treat ATOM_PAIR type similar to list of atoms. */
1575 || type == dpyinfo->Xatom_ATOM_PAIR)
1577 ptrdiff_t i;
1578 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1579 But the callers of these function has made sure the data for
1580 format == 32 is an array of int. Thus, use int instead
1581 of Atom. */
1582 int *idata = (int *) data;
1584 if (size == sizeof (int))
1585 return x_atom_to_symbol (dpyinfo, (Atom) idata[0]);
1586 else
1588 Lisp_Object v = make_uninit_vector (size / sizeof (int));
1590 for (i = 0; i < size / sizeof (int); i++)
1591 ASET (v, i, x_atom_to_symbol (dpyinfo, (Atom) idata[i]));
1592 return v;
1596 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1597 If the number is 32 bits and won't fit in a Lisp_Int,
1598 convert it to a cons of integers, 16 bits in each half.
1600 else if (format == 32 && size == sizeof (int))
1601 return INTEGER_TO_CONS (((int *) data) [0]);
1602 else if (format == 16 && size == sizeof (short))
1603 return make_number (((short *) data) [0]);
1605 /* Convert any other kind of data to a vector of numbers, represented
1606 as above (as an integer, or a cons of two 16 bit integers.)
1608 else if (format == 16)
1610 ptrdiff_t i;
1611 Lisp_Object v = make_uninit_vector (size / 2);
1613 for (i = 0; i < size / 2; i++)
1615 short j = ((short *) data) [i];
1616 ASET (v, i, make_number (j));
1618 return v;
1620 else
1622 ptrdiff_t i;
1623 Lisp_Object v = make_uninit_vector (size / X_LONG_SIZE);
1625 for (i = 0; i < size / X_LONG_SIZE; i++)
1627 int j = ((int *) data) [i];
1628 ASET (v, i, INTEGER_TO_CONS (j));
1630 return v;
1634 /* Convert OBJ to an X long value, and return it as unsigned long.
1635 OBJ should be an integer or a cons representing an integer.
1636 Treat values in the range X_LONG_MAX + 1 .. X_ULONG_MAX as X
1637 unsigned long values: in theory these values are supposed to be
1638 signed but in practice unsigned 32-bit data are communicated via X
1639 selections and we need to support that. */
1640 static unsigned long
1641 cons_to_x_long (Lisp_Object obj)
1643 if (X_ULONG_MAX <= INTMAX_MAX
1644 || XINT (INTEGERP (obj) ? obj : XCAR (obj)) < 0)
1645 return cons_to_signed (obj, X_LONG_MIN, min (X_ULONG_MAX, INTMAX_MAX));
1646 else
1647 return cons_to_unsigned (obj, X_ULONG_MAX);
1650 /* Use xfree, not XFree, to free the data obtained with this function. */
1652 static void
1653 lisp_data_to_selection_data (struct x_display_info *dpyinfo,
1654 Lisp_Object obj, struct selection_data *cs)
1656 Lisp_Object type = Qnil;
1658 eassert (cs != NULL);
1659 cs->nofree = false;
1661 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1663 type = XCAR (obj);
1664 obj = XCDR (obj);
1665 if (CONSP (obj) && NILP (XCDR (obj)))
1666 obj = XCAR (obj);
1669 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1670 { /* This is not the same as declining */
1671 cs->format = 32;
1672 cs->size = 0;
1673 cs->data = NULL;
1674 type = QNULL;
1676 else if (STRINGP (obj))
1678 if (SCHARS (obj) < SBYTES (obj))
1679 /* OBJ is a multibyte string containing a non-ASCII char. */
1680 signal_error ("Non-ASCII string must be encoded in advance", obj);
1681 if (NILP (type))
1682 type = QSTRING;
1683 cs->format = 8;
1684 cs->size = SBYTES (obj);
1685 cs->data = SDATA (obj);
1686 cs->nofree = true;
1688 else if (SYMBOLP (obj))
1690 void *data = xmalloc (sizeof (Atom) + 1);
1691 Atom *x_atom_ptr = data;
1692 cs->data = data;
1693 cs->format = 32;
1694 cs->size = 1;
1695 cs->data[sizeof (Atom)] = 0;
1696 *x_atom_ptr = symbol_to_x_atom (dpyinfo, obj);
1697 if (NILP (type)) type = QATOM;
1699 else if (RANGED_INTEGERP (X_SHRT_MIN, obj, X_SHRT_MAX))
1701 void *data = xmalloc (sizeof (short) + 1);
1702 short *short_ptr = data;
1703 cs->data = data;
1704 cs->format = 16;
1705 cs->size = 1;
1706 cs->data[sizeof (short)] = 0;
1707 *short_ptr = XINT (obj);
1708 if (NILP (type)) type = QINTEGER;
1710 else if (INTEGERP (obj)
1711 || (CONSP (obj) && INTEGERP (XCAR (obj))
1712 && (INTEGERP (XCDR (obj))
1713 || (CONSP (XCDR (obj))
1714 && INTEGERP (XCAR (XCDR (obj)))))))
1716 void *data = xmalloc (sizeof (unsigned long) + 1);
1717 unsigned long *x_long_ptr = data;
1718 cs->data = data;
1719 cs->format = 32;
1720 cs->size = 1;
1721 cs->data[sizeof (unsigned long)] = 0;
1722 *x_long_ptr = cons_to_x_long (obj);
1723 if (NILP (type)) type = QINTEGER;
1725 else if (VECTORP (obj))
1727 /* Lisp_Vectors may represent a set of ATOMs;
1728 a set of 16 or 32 bit INTEGERs;
1729 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1731 ptrdiff_t i;
1732 ptrdiff_t size = ASIZE (obj);
1734 if (SYMBOLP (AREF (obj, 0)))
1735 /* This vector is an ATOM set */
1737 void *data;
1738 Atom *x_atoms;
1739 if (NILP (type)) type = QATOM;
1740 for (i = 0; i < size; i++)
1741 if (!SYMBOLP (AREF (obj, i)))
1742 signal_error ("All elements of selection vector must have same type", obj);
1744 cs->data = data = xnmalloc (size, sizeof *x_atoms);
1745 x_atoms = data;
1746 cs->format = 32;
1747 cs->size = size;
1748 for (i = 0; i < size; i++)
1749 x_atoms[i] = symbol_to_x_atom (dpyinfo, AREF (obj, i));
1751 else
1752 /* This vector is an INTEGER set, or something like it */
1754 int format = 16;
1755 int data_size = sizeof (short);
1756 void *data;
1757 unsigned long *x_atoms;
1758 short *shorts;
1759 if (NILP (type)) type = QINTEGER;
1760 for (i = 0; i < size; i++)
1762 if (! RANGED_INTEGERP (X_SHRT_MIN, AREF (obj, i),
1763 X_SHRT_MAX))
1765 /* Use sizeof (long) even if it is more than 32 bits.
1766 See comment in x_get_window_property and
1767 x_fill_property_data. */
1768 data_size = sizeof (long);
1769 format = 32;
1770 break;
1773 cs->data = data = xnmalloc (size, data_size);
1774 x_atoms = data;
1775 shorts = data;
1776 cs->format = format;
1777 cs->size = size;
1778 for (i = 0; i < size; i++)
1780 if (format == 32)
1781 x_atoms[i] = cons_to_x_long (AREF (obj, i));
1782 else
1783 shorts[i] = XINT (AREF (obj, i));
1787 else
1788 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1790 cs->type = symbol_to_x_atom (dpyinfo, type);
1793 static Lisp_Object
1794 clean_local_selection_data (Lisp_Object obj)
1796 if (CONSP (obj)
1797 && INTEGERP (XCAR (obj))
1798 && CONSP (XCDR (obj))
1799 && INTEGERP (XCAR (XCDR (obj)))
1800 && NILP (XCDR (XCDR (obj))))
1801 obj = Fcons (XCAR (obj), XCDR (obj));
1803 if (CONSP (obj)
1804 && INTEGERP (XCAR (obj))
1805 && INTEGERP (XCDR (obj)))
1807 if (XINT (XCAR (obj)) == 0)
1808 return XCDR (obj);
1809 if (XINT (XCAR (obj)) == -1)
1810 return make_number (- XINT (XCDR (obj)));
1812 if (VECTORP (obj))
1814 ptrdiff_t i;
1815 ptrdiff_t size = ASIZE (obj);
1816 Lisp_Object copy;
1817 if (size == 1)
1818 return clean_local_selection_data (AREF (obj, 0));
1819 copy = make_uninit_vector (size);
1820 for (i = 0; i < size; i++)
1821 ASET (copy, i, clean_local_selection_data (AREF (obj, i)));
1822 return copy;
1824 return obj;
1827 /* Called from XTread_socket to handle SelectionNotify events.
1828 If it's the selection we are waiting for, stop waiting
1829 by setting the car of reading_selection_reply to non-nil.
1830 We store t there if the reply is successful, lambda if not. */
1832 void
1833 x_handle_selection_notify (const XSelectionEvent *event)
1835 if (event->requestor != reading_selection_window)
1836 return;
1837 if (event->selection != reading_which_selection)
1838 return;
1840 TRACE0 ("Received SelectionNotify");
1841 XSETCAR (reading_selection_reply,
1842 (event->property != 0 ? Qt : Qlambda));
1846 /* From a Lisp_Object, return a suitable frame for selection
1847 operations. OBJECT may be a frame, a terminal object, or nil
1848 (which stands for the selected frame--or, if that is not an X
1849 frame, the first X display on the list). If no suitable frame can
1850 be found, return NULL. */
1852 static struct frame *
1853 frame_for_x_selection (Lisp_Object object)
1855 Lisp_Object tail, frame;
1856 struct frame *f;
1858 if (NILP (object))
1860 f = XFRAME (selected_frame);
1861 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1862 return f;
1864 FOR_EACH_FRAME (tail, frame)
1866 f = XFRAME (frame);
1867 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1868 return f;
1871 else if (TERMINALP (object))
1873 struct terminal *t = decode_live_terminal (object);
1875 if (t->type == output_x_window)
1876 FOR_EACH_FRAME (tail, frame)
1878 f = XFRAME (frame);
1879 if (FRAME_LIVE_P (f) && f->terminal == t)
1880 return f;
1883 else if (FRAMEP (object))
1885 f = XFRAME (object);
1886 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1887 return f;
1890 return NULL;
1894 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1895 Sx_own_selection_internal, 2, 3, 0,
1896 doc: /* Assert an X selection of type SELECTION and value VALUE.
1897 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1898 \(Those are literal upper-case symbol names, since that's what X expects.)
1899 VALUE is typically a string, or a cons of two markers, but may be
1900 anything that the functions on `selection-converter-alist' know about.
1902 FRAME should be a frame that should own the selection. If omitted or
1903 nil, it defaults to the selected frame.
1905 On Nextstep, FRAME is unused. */)
1906 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1908 if (NILP (frame)) frame = selected_frame;
1909 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1910 error ("X selection unavailable for this frame");
1912 CHECK_SYMBOL (selection);
1913 if (NILP (value)) error ("VALUE may not be nil");
1914 x_own_selection (selection, value, frame);
1915 return value;
1919 /* Request the selection value from the owner. If we are the owner,
1920 simply return our selection value. If we are not the owner, this
1921 will block until all of the data has arrived. */
1923 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1924 Sx_get_selection_internal, 2, 4, 0,
1925 doc: /* Return text selected from some X window.
1926 SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1927 \(Those are literal upper-case symbol names, since that's what X expects.)
1928 TARGET-TYPE is the type of data desired, typically `STRING'.
1930 TIME-STAMP is the time to use in the XConvertSelection call for foreign
1931 selections. If omitted, defaults to the time for the last event.
1933 TERMINAL should be a terminal object or a frame specifying the X
1934 server to query. If omitted or nil, that stands for the selected
1935 frame's display, or the first available X display.
1937 On Nextstep, TIME-STAMP and TERMINAL are unused. */)
1938 (Lisp_Object selection_symbol, Lisp_Object target_type,
1939 Lisp_Object time_stamp, Lisp_Object terminal)
1941 Lisp_Object val = Qnil;
1942 struct gcpro gcpro1, gcpro2;
1943 struct frame *f = frame_for_x_selection (terminal);
1944 GCPRO2 (target_type, val); /* we store newly consed data into these */
1946 CHECK_SYMBOL (selection_symbol);
1947 CHECK_SYMBOL (target_type);
1948 if (EQ (target_type, QMULTIPLE))
1949 error ("Retrieving MULTIPLE selections is currently unimplemented");
1950 if (!f)
1951 error ("X selection unavailable for this frame");
1953 val = x_get_local_selection (selection_symbol, target_type, true,
1954 FRAME_DISPLAY_INFO (f));
1956 if (NILP (val) && FRAME_LIVE_P (f))
1958 Lisp_Object frame;
1959 XSETFRAME (frame, f);
1960 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1961 time_stamp, frame));
1964 if (CONSP (val) && SYMBOLP (XCAR (val)))
1966 val = XCDR (val);
1967 if (CONSP (val) && NILP (XCDR (val)))
1968 val = XCAR (val);
1970 RETURN_UNGCPRO (clean_local_selection_data (val));
1973 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
1974 Sx_disown_selection_internal, 1, 3, 0,
1975 doc: /* If we own the selection SELECTION, disown it.
1976 Disowning it means there is no such selection.
1978 Sets the last-change time for the selection to TIME-OBJECT (by default
1979 the time of the last event).
1981 TERMINAL should be a terminal object or a frame specifying the X
1982 server to query. If omitted or nil, that stands for the selected
1983 frame's display, or the first available X display.
1985 On Nextstep, the TIME-OBJECT and TERMINAL arguments are unused.
1986 On MS-DOS, all this does is return non-nil if we own the selection. */)
1987 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
1989 Time timestamp;
1990 Atom selection_atom;
1991 union {
1992 struct selection_input_event sie;
1993 struct input_event ie;
1994 } event;
1995 struct frame *f = frame_for_x_selection (terminal);
1996 struct x_display_info *dpyinfo;
1998 if (!f)
1999 return Qnil;
2001 dpyinfo = FRAME_DISPLAY_INFO (f);
2002 CHECK_SYMBOL (selection);
2004 /* Don't disown the selection when we're not the owner. */
2005 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2006 return Qnil;
2008 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2010 block_input ();
2011 if (NILP (time_object))
2012 timestamp = dpyinfo->last_user_time;
2013 else
2014 CONS_TO_INTEGER (time_object, Time, timestamp);
2015 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2016 unblock_input ();
2018 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2019 generated for a window which owns the selection when that window sets
2020 the selection owner to None. The NCD server does, the MIT Sun4 server
2021 doesn't. So we synthesize one; this means we might get two, but
2022 that's ok, because the second one won't have any effect. */
2023 SELECTION_EVENT_DPYINFO (&event.sie) = dpyinfo;
2024 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2025 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2026 x_handle_selection_clear (&event.ie);
2028 return Qt;
2031 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2032 0, 2, 0,
2033 doc: /* Whether the current Emacs process owns the given X Selection.
2034 The arg should be the name of the selection in question, typically one of
2035 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2036 \(Those are literal upper-case symbol names, since that's what X expects.)
2037 For convenience, the symbol nil is the same as `PRIMARY',
2038 and t is the same as `SECONDARY'.
2040 TERMINAL should be a terminal object or a frame specifying the X
2041 server to query. If omitted or nil, that stands for the selected
2042 frame's display, or the first available X display.
2044 On Nextstep, TERMINAL is unused. */)
2045 (Lisp_Object selection, Lisp_Object terminal)
2047 struct frame *f = frame_for_x_selection (terminal);
2049 CHECK_SYMBOL (selection);
2050 if (EQ (selection, Qnil)) selection = QPRIMARY;
2051 if (EQ (selection, Qt)) selection = QSECONDARY;
2053 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_DISPLAY_INFO (f))))
2054 return Qt;
2055 else
2056 return Qnil;
2059 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2060 0, 2, 0,
2061 doc: /* Whether there is an owner for the given X selection.
2062 SELECTION should be the name of the selection in question, typically
2063 one of the symbols `PRIMARY', `SECONDARY', `CLIPBOARD', or
2064 `CLIPBOARD_MANAGER' (X expects these literal upper-case names.) The
2065 symbol nil is the same as `PRIMARY', and t is the same as `SECONDARY'.
2067 TERMINAL should be a terminal object or a frame specifying the X
2068 server to query. If omitted or nil, that stands for the selected
2069 frame's display, or the first available X display.
2071 On Nextstep, TERMINAL is unused. */)
2072 (Lisp_Object selection, Lisp_Object terminal)
2074 Window owner;
2075 Atom atom;
2076 struct frame *f = frame_for_x_selection (terminal);
2077 struct x_display_info *dpyinfo;
2079 CHECK_SYMBOL (selection);
2080 if (EQ (selection, Qnil)) selection = QPRIMARY;
2081 if (EQ (selection, Qt)) selection = QSECONDARY;
2083 if (!f)
2084 return Qnil;
2086 dpyinfo = FRAME_DISPLAY_INFO (f);
2088 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2089 return Qt;
2091 atom = symbol_to_x_atom (dpyinfo, selection);
2092 if (atom == 0) return Qnil;
2093 block_input ();
2094 owner = XGetSelectionOwner (dpyinfo->display, atom);
2095 unblock_input ();
2096 return (owner ? Qt : Qnil);
2100 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2101 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2103 static Lisp_Object
2104 x_clipboard_manager_save (Lisp_Object frame)
2106 struct frame *f = XFRAME (frame);
2107 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2108 Atom data = dpyinfo->Xatom_UTF8_STRING;
2110 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2111 dpyinfo->Xatom_EMACS_TMP,
2112 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2113 (unsigned char *) &data, 1);
2114 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2115 Qnil, frame);
2116 return Qt;
2119 /* Error handler for x_clipboard_manager_save_frame. */
2121 static Lisp_Object
2122 x_clipboard_manager_error_1 (Lisp_Object err)
2124 AUTO_STRING (format, "X clipboard manager error: %s\n\
2125 If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2126 CALLN (Fmessage, format, CAR (CDR (err)));
2127 return Qnil;
2130 /* Error handler for x_clipboard_manager_save_all. */
2132 static Lisp_Object
2133 x_clipboard_manager_error_2 (Lisp_Object err)
2135 fprintf (stderr, "Error saving to X clipboard manager.\n\
2136 If the problem persists, set `x-select-enable-clipboard-manager' \
2137 to nil.\n");
2138 return Qnil;
2141 /* Called from delete_frame: save any clipboard owned by FRAME to the
2142 clipboard manager. Do nothing if FRAME does not own the clipboard,
2143 or if no clipboard manager is present. */
2145 void
2146 x_clipboard_manager_save_frame (Lisp_Object frame)
2148 struct frame *f;
2150 if (!NILP (Vx_select_enable_clipboard_manager)
2151 && FRAMEP (frame)
2152 && (f = XFRAME (frame), FRAME_X_P (f))
2153 && FRAME_LIVE_P (f))
2155 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2156 Lisp_Object local_selection
2157 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2159 if (!NILP (local_selection)
2160 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2161 && XGetSelectionOwner (dpyinfo->display,
2162 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2163 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2164 x_clipboard_manager_error_1);
2168 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2169 clipboard manager. Do nothing if FRAME does not own the clipboard,
2170 or if no clipboard manager is present. */
2172 void
2173 x_clipboard_manager_save_all (void)
2175 /* Loop through all X displays, saving owned clipboards. */
2176 struct x_display_info *dpyinfo;
2177 Lisp_Object local_selection, local_frame;
2179 if (NILP (Vx_select_enable_clipboard_manager))
2180 return;
2182 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2184 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2185 if (NILP (local_selection)
2186 || !XGetSelectionOwner (dpyinfo->display,
2187 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2188 continue;
2190 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2191 if (FRAME_LIVE_P (XFRAME (local_frame)))
2193 AUTO_STRING (saving, "Saving clipboard to X clipboard manager...");
2194 Fmessage (1, &saving);
2195 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2196 Qt, x_clipboard_manager_error_2);
2202 /***********************************************************************
2203 Drag and drop support
2204 ***********************************************************************/
2205 /* Check that lisp values are of correct type for x_fill_property_data.
2206 That is, number, string or a cons with two numbers (low and high 16
2207 bit parts of a 32 bit number). Return the number of items in DATA,
2208 or -1 if there is an error. */
2211 x_check_property_data (Lisp_Object data)
2213 Lisp_Object iter;
2214 int size = 0;
2216 for (iter = data; CONSP (iter); iter = XCDR (iter))
2218 Lisp_Object o = XCAR (iter);
2220 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2221 return -1;
2222 else if (CONSP (o) &&
2223 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2224 return -1;
2225 if (size == INT_MAX)
2226 return -1;
2227 size++;
2230 return size;
2233 /* Convert lisp values to a C array. Values may be a number, a string
2234 which is taken as an X atom name and converted to the atom value, or
2235 a cons containing the two 16 bit parts of a 32 bit number.
2237 DPY is the display use to look up X atoms.
2238 DATA is a Lisp list of values to be converted.
2239 RET is the C array that contains the converted values. It is assumed
2240 it is big enough to hold all values.
2241 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2242 be stored in RET. Note that long is used for 32 even if long is more
2243 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2244 XClientMessageEvent). */
2246 void
2247 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2249 unsigned long val;
2250 unsigned long *d32 = (unsigned long *) ret;
2251 unsigned short *d16 = (unsigned short *) ret;
2252 unsigned char *d08 = (unsigned char *) ret;
2253 Lisp_Object iter;
2255 for (iter = data; CONSP (iter); iter = XCDR (iter))
2257 Lisp_Object o = XCAR (iter);
2259 if (INTEGERP (o) || FLOATP (o) || CONSP (o))
2261 if (CONSP (o)
2262 && RANGED_INTEGERP (X_LONG_MIN >> 16, XCAR (o), X_LONG_MAX >> 16)
2263 && RANGED_INTEGERP (- (1 << 15), XCDR (o), -1))
2265 /* cons_to_x_long does not handle negative values for v2.
2266 For XDnd, v2 might be y of a window, and can be negative.
2267 The XDnd spec. is not explicit about negative values,
2268 but let's assume negative v2 is sent modulo 2**16. */
2269 unsigned long v1 = XINT (XCAR (o)) & 0xffff;
2270 unsigned long v2 = XINT (XCDR (o)) & 0xffff;
2271 val = (v1 << 16) | v2;
2273 else
2274 val = cons_to_x_long (o);
2276 else if (STRINGP (o))
2278 block_input ();
2279 val = XInternAtom (dpy, SSDATA (o), False);
2280 unblock_input ();
2282 else
2283 error ("Wrong type, must be string, number or cons");
2285 if (format == 8)
2287 if ((1 << 8) < val && val <= X_ULONG_MAX - (1 << 7))
2288 error ("Out of 'char' range");
2289 *d08++ = val;
2291 else if (format == 16)
2293 if ((1 << 16) < val && val <= X_ULONG_MAX - (1 << 15))
2294 error ("Out of 'short' range");
2295 *d16++ = val;
2297 else
2298 *d32++ = val;
2302 /* Convert an array of C values to a Lisp list.
2303 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2304 DATA is a C array of values to be converted.
2305 TYPE is the type of the data. Only XA_ATOM is special, it converts
2306 each number in DATA to its corresponding X atom as a symbol.
2307 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2308 be stored in RET.
2309 SIZE is the number of elements in DATA.
2311 Important: When format is 32, data should contain an array of int,
2312 not an array of long as the X library returns. This makes a difference
2313 when sizeof(long) != sizeof(int).
2315 Also see comment for selection_data_to_lisp_data above. */
2317 Lisp_Object
2318 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2319 Atom type, int format, unsigned long size)
2321 ptrdiff_t format_bytes = format >> 3;
2322 if (PTRDIFF_MAX / format_bytes < size)
2323 memory_full (SIZE_MAX);
2324 return selection_data_to_lisp_data (FRAME_DISPLAY_INFO (f), data,
2325 size * format_bytes, type, format);
2328 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2329 Sx_get_atom_name, 1, 2, 0,
2330 doc: /* Return the X atom name for VALUE as a string.
2331 VALUE may be a number or a cons where the car is the upper 16 bits and
2332 the cdr is the lower 16 bits of a 32 bit value.
2333 Use the display for FRAME or the current frame if FRAME is not given or nil.
2335 If the value is 0 or the atom is not known, return the empty string. */)
2336 (Lisp_Object value, Lisp_Object frame)
2338 struct frame *f = decode_window_system_frame (frame);
2339 char *name = 0;
2340 char empty[] = "";
2341 Lisp_Object ret = Qnil;
2342 Display *dpy = FRAME_X_DISPLAY (f);
2343 Atom atom;
2344 bool had_errors_p;
2346 CONS_TO_INTEGER (value, Atom, atom);
2348 block_input ();
2349 x_catch_errors (dpy);
2350 name = atom ? XGetAtomName (dpy, atom) : empty;
2351 had_errors_p = x_had_errors_p (dpy);
2352 x_uncatch_errors ();
2354 if (!had_errors_p)
2355 ret = build_string (name);
2357 if (atom && name) XFree (name);
2358 if (NILP (ret)) ret = empty_unibyte_string;
2360 unblock_input ();
2362 return ret;
2365 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2366 Sx_register_dnd_atom, 1, 2, 0,
2367 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2368 ATOM can be a symbol or a string. The ATOM is interned on the display that
2369 FRAME is on. If FRAME is nil, the selected frame is used. */)
2370 (Lisp_Object atom, Lisp_Object frame)
2372 Atom x_atom;
2373 struct frame *f = decode_window_system_frame (frame);
2374 ptrdiff_t i;
2375 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2378 if (SYMBOLP (atom))
2379 x_atom = symbol_to_x_atom (dpyinfo, atom);
2380 else if (STRINGP (atom))
2382 block_input ();
2383 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2384 unblock_input ();
2386 else
2387 error ("ATOM must be a symbol or a string");
2389 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2390 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2391 return Qnil;
2393 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2394 dpyinfo->x_dnd_atoms =
2395 xpalloc (dpyinfo->x_dnd_atoms, &dpyinfo->x_dnd_atoms_size,
2396 1, -1, sizeof *dpyinfo->x_dnd_atoms);
2398 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2399 return Qnil;
2402 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2404 bool
2405 x_handle_dnd_message (struct frame *f, const XClientMessageEvent *event,
2406 struct x_display_info *dpyinfo, struct input_event *bufp)
2408 Lisp_Object vec;
2409 Lisp_Object frame;
2410 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2411 unsigned long size = 160/event->format;
2412 int x, y;
2413 unsigned char *data = (unsigned char *) event->data.b;
2414 int idata[5];
2415 ptrdiff_t i;
2417 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2418 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2420 if (i == dpyinfo->x_dnd_atoms_length) return false;
2422 XSETFRAME (frame, f);
2424 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2425 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2426 function expects them to be of size int (i.e. 32). So to be able to
2427 use that function, put the data in the form it expects if format is 32. */
2429 if (BITS_PER_LONG > 32 && event->format == 32)
2431 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2432 idata[i] = event->data.l[i];
2433 data = (unsigned char *) idata;
2436 vec = Fmake_vector (make_number (4), Qnil);
2437 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_DISPLAY_INFO (f),
2438 event->message_type)));
2439 ASET (vec, 1, frame);
2440 ASET (vec, 2, make_number (event->format));
2441 ASET (vec, 3, x_property_data_to_lisp (f,
2442 data,
2443 event->message_type,
2444 event->format,
2445 size));
2447 x_relative_mouse_position (f, &x, &y);
2448 bufp->kind = DRAG_N_DROP_EVENT;
2449 bufp->frame_or_window = frame;
2450 bufp->timestamp = CurrentTime;
2451 bufp->x = make_number (x);
2452 bufp->y = make_number (y);
2453 bufp->arg = vec;
2454 bufp->modifiers = 0;
2456 return true;
2459 DEFUN ("x-send-client-message", Fx_send_client_message,
2460 Sx_send_client_message, 6, 6, 0,
2461 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2463 For DISPLAY, specify either a frame or a display name (a string).
2464 If DISPLAY is nil, that stands for the selected frame's display.
2465 DEST may be a number, in which case it is a Window id. The value 0 may
2466 be used to send to the root window of the DISPLAY.
2467 If DEST is a cons, it is converted to a 32 bit number
2468 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2469 number is then used as a window id.
2470 If DEST is a frame the event is sent to the outer window of that frame.
2471 A value of nil means the currently selected frame.
2472 If DEST is the string "PointerWindow" the event is sent to the window that
2473 contains the pointer. If DEST is the string "InputFocus" the event is
2474 sent to the window that has the input focus.
2475 FROM is the frame sending the event. Use nil for currently selected frame.
2476 MESSAGE-TYPE is the name of an Atom as a string.
2477 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2478 bits. VALUES is a list of numbers, cons and/or strings containing the values
2479 to send. If a value is a string, it is converted to an Atom and the value of
2480 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2481 with the high 16 bits from the car and the lower 16 bit from the cdr.
2482 If more values than fits into the event is given, the excessive values
2483 are ignored. */)
2484 (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2485 Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2487 struct x_display_info *dpyinfo = check_x_display_info (display);
2489 CHECK_STRING (message_type);
2490 x_send_client_event (display, dest, from,
2491 XInternAtom (dpyinfo->display,
2492 SSDATA (message_type),
2493 False),
2494 format, values);
2496 return Qnil;
2499 void
2500 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2501 Atom message_type, Lisp_Object format, Lisp_Object values)
2503 struct x_display_info *dpyinfo = check_x_display_info (display);
2504 Window wdest;
2505 XEvent event;
2506 struct frame *f = decode_window_system_frame (from);
2507 bool to_root;
2509 CHECK_NUMBER (format);
2510 CHECK_CONS (values);
2512 if (x_check_property_data (values) == -1)
2513 error ("Bad data in VALUES, must be number, cons or string");
2515 if (XINT (format) != 8 && XINT (format) != 16 && XINT (format) != 32)
2516 error ("FORMAT must be one of 8, 16 or 32");
2518 event.xclient.type = ClientMessage;
2519 event.xclient.format = XINT (format);
2521 if (FRAMEP (dest) || NILP (dest))
2523 struct frame *fdest = decode_window_system_frame (dest);
2524 wdest = FRAME_OUTER_WINDOW (fdest);
2526 else if (STRINGP (dest))
2528 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2529 wdest = PointerWindow;
2530 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2531 wdest = InputFocus;
2532 else
2533 error ("DEST as a string must be one of PointerWindow or InputFocus");
2535 else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest))
2536 CONS_TO_INTEGER (dest, Window, wdest);
2537 else
2538 error ("DEST must be a frame, nil, string, number or cons");
2540 if (wdest == 0) wdest = dpyinfo->root_window;
2541 to_root = wdest == dpyinfo->root_window;
2543 block_input ();
2545 event.xclient.send_event = True;
2546 event.xclient.serial = 0;
2547 event.xclient.message_type = message_type;
2548 event.xclient.display = dpyinfo->display;
2550 /* Some clients (metacity for example) expects sending window to be here
2551 when sending to the root window. */
2552 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2554 memset (event.xclient.data.l, 0, sizeof (event.xclient.data.l));
2555 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2556 event.xclient.format);
2558 /* If event mask is 0 the event is sent to the client that created
2559 the destination window. But if we are sending to the root window,
2560 there is no such client. Then we set the event mask to 0xffffff. The
2561 event then goes to clients selecting for events on the root window. */
2562 x_catch_errors (dpyinfo->display);
2564 bool propagate = !to_root;
2565 long mask = to_root ? 0xffffff : 0;
2567 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2568 XFlush (dpyinfo->display);
2570 x_uncatch_errors ();
2571 unblock_input ();
2575 void
2576 syms_of_xselect (void)
2578 defsubr (&Sx_get_selection_internal);
2579 defsubr (&Sx_own_selection_internal);
2580 defsubr (&Sx_disown_selection_internal);
2581 defsubr (&Sx_selection_owner_p);
2582 defsubr (&Sx_selection_exists_p);
2584 defsubr (&Sx_get_atom_name);
2585 defsubr (&Sx_send_client_message);
2586 defsubr (&Sx_register_dnd_atom);
2588 reading_selection_reply = Fcons (Qnil, Qnil);
2589 staticpro (&reading_selection_reply);
2590 reading_selection_window = 0;
2591 reading_which_selection = 0;
2593 property_change_wait_list = 0;
2594 prop_location_identifier = 0;
2595 property_change_reply = Fcons (Qnil, Qnil);
2596 staticpro (&property_change_reply);
2598 converted_selections = NULL;
2599 conversion_fail_tag = None;
2601 /* FIXME: Duplicate definition in nsselect.c. */
2602 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2603 doc: /* An alist associating X Windows selection-types with functions.
2604 These functions are called to convert the selection, with three args:
2605 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2606 a desired type to which the selection should be converted;
2607 and the local selection value (whatever was given to
2608 `x-own-selection-internal').
2610 The function should return the value to send to the X server
2611 \(typically a string). A return value of nil
2612 means that the conversion could not be done.
2613 A return value which is the symbol `NULL'
2614 means that a side-effect was executed,
2615 and there is no meaningful selection value. */);
2616 Vselection_converter_alist = Qnil;
2618 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2619 doc: /* A list of functions to be called when Emacs loses an X selection.
2620 \(This happens when some other X client makes its own selection
2621 or when a Lisp program explicitly clears the selection.)
2622 The functions are called with one argument, the selection type
2623 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2624 Vx_lost_selection_functions = Qnil;
2626 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2627 doc: /* A list of functions to be called when Emacs answers a selection request.
2628 The functions are called with three arguments:
2629 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2630 - the selection-type which Emacs was asked to convert the
2631 selection into before sending (for example, `STRING' or `LENGTH');
2632 - a flag indicating success or failure for responding to the request.
2633 We might have failed (and declined the request) for any number of reasons,
2634 including being asked for a selection that we no longer own, or being asked
2635 to convert into a type that we don't know about or that is inappropriate.
2636 This hook doesn't let you change the behavior of Emacs's selection replies,
2637 it merely informs you that they have happened. */);
2638 Vx_sent_selection_functions = Qnil;
2640 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2641 Vx_select_enable_clipboard_manager,
2642 doc: /* Whether to enable X clipboard manager support.
2643 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2644 while owning the X clipboard, the clipboard contents are saved to the
2645 clipboard manager if one is present. */);
2646 Vx_select_enable_clipboard_manager = Qt;
2648 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2649 doc: /* Number of milliseconds to wait for a selection reply.
2650 If the selection owner doesn't reply in this time, we give up.
2651 A value of 0 means wait as long as necessary. This is initialized from the
2652 \"*selectionTimeout\" resource. */);
2653 x_selection_timeout = 0;
2655 /* QPRIMARY is defined in keyboard.c. */
2656 DEFSYM (QSECONDARY, "SECONDARY");
2657 DEFSYM (QSTRING, "STRING");
2658 DEFSYM (QINTEGER, "INTEGER");
2659 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2660 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2661 DEFSYM (QTEXT, "TEXT");
2663 /* These are types of selection. */
2664 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2665 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2667 DEFSYM (QDELETE, "DELETE");
2668 DEFSYM (QMULTIPLE, "MULTIPLE");
2669 DEFSYM (QINCR, "INCR");
2670 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2671 DEFSYM (QTARGETS, "TARGETS");
2672 DEFSYM (QATOM, "ATOM");
2673 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2674 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2675 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2676 DEFSYM (QNULL, "NULL");
2677 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2678 DEFSYM (Qforeign_selection, "foreign-selection");
2679 DEFSYM (Qx_lost_selection_functions, "x-lost-selection-functions");
2680 DEFSYM (Qx_sent_selection_functions, "x-sent-selection-functions");