Support X clipboard managers.
[emacs.git] / src / xselect.c
blobf0ffbe0eb2d96b0bc95981c94e69e54a24628e1a
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2011 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 <stdio.h> /* termhooks.h needs this */
24 #include <setjmp.h>
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 "buffer.h"
38 #include "process.h"
39 #include "termhooks.h"
40 #include "keyboard.h"
41 #include "character.h"
43 #include <X11/Xproto.h>
45 struct prop_location;
46 struct selection_data;
48 static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
49 static Atom symbol_to_x_atom (struct x_display_info *, Lisp_Object);
50 static void x_own_selection (Lisp_Object, Lisp_Object, Lisp_Object);
51 static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int,
52 struct x_display_info *);
53 static void x_decline_selection_request (struct input_event *);
54 static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
55 static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
56 static Lisp_Object x_catch_errors_unwind (Lisp_Object);
57 static void x_reply_selection_request (struct input_event *, struct x_display_info *);
58 static int x_convert_selection (struct input_event *, Lisp_Object, Lisp_Object,
59 Atom, int, struct x_display_info *);
60 static int waiting_for_other_props_on_window (Display *, Window);
61 static struct prop_location *expect_property_change (Display *, Window,
62 Atom, int);
63 static void unexpect_property_change (struct prop_location *);
64 static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
65 static void wait_for_property_change (struct prop_location *);
66 static Lisp_Object x_get_foreign_selection (Lisp_Object, Lisp_Object,
67 Lisp_Object, Lisp_Object);
68 static void x_get_window_property (Display *, Window, Atom,
69 unsigned char **, int *,
70 Atom *, int *, unsigned long *, int);
71 static void receive_incremental_selection (Display *, Window, Atom,
72 Lisp_Object, unsigned,
73 unsigned char **, int *,
74 Atom *, int *, unsigned long *);
75 static Lisp_Object x_get_window_property_as_lisp_data (Display *,
76 Window, Atom,
77 Lisp_Object, Atom);
78 static Lisp_Object selection_data_to_lisp_data (Display *,
79 const unsigned char *,
80 int, Atom, int);
81 static void lisp_data_to_selection_data (Display *, Lisp_Object,
82 unsigned char **, Atom *,
83 unsigned *, int *, int *);
84 static Lisp_Object clean_local_selection_data (Lisp_Object);
86 /* Printing traces to stderr. */
88 #ifdef TRACE_SELECTION
89 #define TRACE0(fmt) \
90 fprintf (stderr, "%d: " fmt "\n", getpid ())
91 #define TRACE1(fmt, a0) \
92 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
93 #define TRACE2(fmt, a0, a1) \
94 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
95 #define TRACE3(fmt, a0, a1, a2) \
96 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
97 #else
98 #define TRACE0(fmt) (void) 0
99 #define TRACE1(fmt, a0) (void) 0
100 #define TRACE2(fmt, a0, a1) (void) 0
101 #endif
104 static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
105 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
106 QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS;
108 static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
109 static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
111 static Lisp_Object Qcompound_text_with_extensions;
113 static Lisp_Object Qforeign_selection;
115 /* If this is a smaller number than the max-request-size of the display,
116 emacs will use INCR selection transfer when the selection is larger
117 than this. The max-request-size is usually around 64k, so if you want
118 emacs to use incremental selection transfers when the selection is
119 smaller than that, set this. I added this mostly for debugging the
120 incremental transfer stuff, but it might improve server performance. */
121 #define MAX_SELECTION_QUANTUM 0xFFFFFF
123 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
125 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
126 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
129 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
130 handling. */
132 struct selection_event_queue
134 struct input_event event;
135 struct selection_event_queue *next;
138 static struct selection_event_queue *selection_queue;
140 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
142 static int x_queue_selection_requests;
144 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
146 static void
147 x_queue_event (struct input_event *event)
149 struct selection_event_queue *queue_tmp;
151 /* Don't queue repeated requests.
152 This only happens for large requests which uses the incremental protocol. */
153 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
155 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
157 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
158 x_decline_selection_request (event);
159 return;
163 queue_tmp
164 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
166 if (queue_tmp != NULL)
168 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
169 queue_tmp->event = *event;
170 queue_tmp->next = selection_queue;
171 selection_queue = queue_tmp;
175 /* Start queuing SELECTION_REQUEST_EVENT events. */
177 static void
178 x_start_queuing_selection_requests (void)
180 if (x_queue_selection_requests)
181 abort ();
183 x_queue_selection_requests++;
184 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
187 /* Stop queuing SELECTION_REQUEST_EVENT events. */
189 static void
190 x_stop_queuing_selection_requests (void)
192 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
193 --x_queue_selection_requests;
195 /* Take all the queued events and put them back
196 so that they get processed afresh. */
198 while (selection_queue != NULL)
200 struct selection_event_queue *queue_tmp = selection_queue;
201 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
202 kbd_buffer_unget_event (&queue_tmp->event);
203 selection_queue = queue_tmp->next;
204 xfree ((char *)queue_tmp);
209 /* This converts a Lisp symbol to a server Atom, avoiding a server
210 roundtrip whenever possible. */
212 static Atom
213 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
215 Atom val;
216 if (NILP (sym)) return 0;
217 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
218 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
219 if (EQ (sym, QSTRING)) return XA_STRING;
220 if (EQ (sym, QINTEGER)) return XA_INTEGER;
221 if (EQ (sym, QATOM)) return XA_ATOM;
222 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
223 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
224 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
225 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
226 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
227 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
228 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
229 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
230 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
231 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
232 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
233 if (!SYMBOLP (sym)) abort ();
235 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
236 BLOCK_INPUT;
237 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
238 UNBLOCK_INPUT;
239 return val;
243 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
244 and calls to intern whenever possible. */
246 static Lisp_Object
247 x_atom_to_symbol (Display *dpy, Atom atom)
249 struct x_display_info *dpyinfo;
250 char *str;
251 Lisp_Object val;
253 if (! atom)
254 return Qnil;
256 switch (atom)
258 case XA_PRIMARY:
259 return QPRIMARY;
260 case XA_SECONDARY:
261 return QSECONDARY;
262 case XA_STRING:
263 return QSTRING;
264 case XA_INTEGER:
265 return QINTEGER;
266 case XA_ATOM:
267 return QATOM;
270 dpyinfo = x_display_info_for_display (dpy);
271 if (dpyinfo == NULL)
272 return Qnil;
273 if (atom == dpyinfo->Xatom_CLIPBOARD)
274 return QCLIPBOARD;
275 if (atom == dpyinfo->Xatom_TIMESTAMP)
276 return QTIMESTAMP;
277 if (atom == dpyinfo->Xatom_TEXT)
278 return QTEXT;
279 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
280 return QCOMPOUND_TEXT;
281 if (atom == dpyinfo->Xatom_UTF8_STRING)
282 return QUTF8_STRING;
283 if (atom == dpyinfo->Xatom_DELETE)
284 return QDELETE;
285 if (atom == dpyinfo->Xatom_MULTIPLE)
286 return QMULTIPLE;
287 if (atom == dpyinfo->Xatom_INCR)
288 return QINCR;
289 if (atom == dpyinfo->Xatom_EMACS_TMP)
290 return QEMACS_TMP;
291 if (atom == dpyinfo->Xatom_TARGETS)
292 return QTARGETS;
293 if (atom == dpyinfo->Xatom_NULL)
294 return QNULL;
296 BLOCK_INPUT;
297 str = XGetAtomName (dpy, atom);
298 UNBLOCK_INPUT;
299 TRACE1 ("XGetAtomName --> %s", str);
300 if (! str) return Qnil;
301 val = intern (str);
302 BLOCK_INPUT;
303 /* This was allocated by Xlib, so use XFree. */
304 XFree (str);
305 UNBLOCK_INPUT;
306 return val;
309 /* Do protocol to assert ourself as a selection owner.
310 FRAME shall be the owner; it must be a valid X frame.
311 Update the Vselection_alist so that we can reply to later requests for
312 our selection. */
314 static void
315 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
316 Lisp_Object frame)
318 struct frame *f = XFRAME (frame);
319 Window selecting_window = FRAME_X_WINDOW (f);
320 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
321 Display *display = dpyinfo->display;
322 Time timestamp = last_event_timestamp;
323 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
325 BLOCK_INPUT;
326 x_catch_errors (display);
327 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
328 x_check_errors (display, "Can't set selection: %s");
329 x_uncatch_errors ();
330 UNBLOCK_INPUT;
332 /* Now update the local cache */
334 Lisp_Object selection_data;
335 Lisp_Object prev_value;
337 selection_data = list4 (selection_name, selection_value,
338 long_to_cons (timestamp), frame);
339 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
341 dpyinfo->terminal->Vselection_alist
342 = Fcons (selection_data, dpyinfo->terminal->Vselection_alist);
344 /* If we already owned the selection, remove the old selection
345 data. Don't use Fdelq as that may QUIT. */
346 if (!NILP (prev_value))
348 /* We know it's not the CAR, so it's easy. */
349 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
350 for (; CONSP (rest); rest = XCDR (rest))
351 if (EQ (prev_value, Fcar (XCDR (rest))))
353 XSETCDR (rest, XCDR (XCDR (rest)));
354 break;
360 /* Given a selection-name and desired type, look up our local copy of
361 the selection value and convert it to the type.
362 The value is nil or a string.
363 This function is used both for remote requests (LOCAL_REQUEST is zero)
364 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
366 This calls random Lisp code, and may signal or gc. */
368 static Lisp_Object
369 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
370 int local_request, struct x_display_info *dpyinfo)
372 Lisp_Object local_value;
373 Lisp_Object handler_fn, value, check;
374 int count;
376 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
378 if (NILP (local_value)) return Qnil;
380 /* TIMESTAMP is a special case. */
381 if (EQ (target_type, QTIMESTAMP))
383 handler_fn = Qnil;
384 value = XCAR (XCDR (XCDR (local_value)));
386 else
388 /* Don't allow a quit within the converter.
389 When the user types C-g, he would be surprised
390 if by luck it came during a converter. */
391 count = SPECPDL_INDEX ();
392 specbind (Qinhibit_quit, Qt);
394 CHECK_SYMBOL (target_type);
395 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
396 /* gcpro is not needed here since nothing but HANDLER_FN
397 is live, and that ought to be a symbol. */
399 if (!NILP (handler_fn))
400 value = call3 (handler_fn,
401 selection_symbol, (local_request ? Qnil : target_type),
402 XCAR (XCDR (local_value)));
403 else
404 value = Qnil;
405 unbind_to (count, Qnil);
408 /* Make sure this value is of a type that we could transmit
409 to another X client. */
411 check = value;
412 if (CONSP (value)
413 && SYMBOLP (XCAR (value)))
414 check = XCDR (value);
416 if (STRINGP (check)
417 || VECTORP (check)
418 || SYMBOLP (check)
419 || INTEGERP (check)
420 || NILP (value))
421 return value;
422 /* Check for a value that cons_to_long could handle. */
423 else if (CONSP (check)
424 && INTEGERP (XCAR (check))
425 && (INTEGERP (XCDR (check))
427 (CONSP (XCDR (check))
428 && INTEGERP (XCAR (XCDR (check)))
429 && NILP (XCDR (XCDR (check))))))
430 return value;
432 signal_error ("Invalid data returned by selection-conversion function",
433 list2 (handler_fn, value));
436 /* Subroutines of x_reply_selection_request. */
438 /* Send a SelectionNotify event to the requestor with property=None,
439 meaning we were unable to do what they wanted. */
441 static void
442 x_decline_selection_request (struct input_event *event)
444 XEvent reply_base;
445 XSelectionEvent *reply = &(reply_base.xselection);
447 reply->type = SelectionNotify;
448 reply->display = SELECTION_EVENT_DISPLAY (event);
449 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
450 reply->selection = SELECTION_EVENT_SELECTION (event);
451 reply->time = SELECTION_EVENT_TIME (event);
452 reply->target = SELECTION_EVENT_TARGET (event);
453 reply->property = None;
455 /* The reason for the error may be that the receiver has
456 died in the meantime. Handle that case. */
457 BLOCK_INPUT;
458 x_catch_errors (reply->display);
459 XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base);
460 XFlush (reply->display);
461 x_uncatch_errors ();
462 UNBLOCK_INPUT;
465 /* This is the selection request currently being processed.
466 It is set to zero when the request is fully processed. */
467 static struct input_event *x_selection_current_request;
469 /* Display info in x_selection_request. */
471 static struct x_display_info *selection_request_dpyinfo;
473 /* Raw selection data, for sending to a requestor window. */
475 struct selection_data
477 unsigned char *data;
478 unsigned int size;
479 int format;
480 Atom type;
481 int nofree;
482 Atom property;
483 /* This can be set to non-NULL during x_reply_selection_request, if
484 the selection is waiting for an INCR transfer to complete. Don't
485 free these; that's done by unexpect_property_change. */
486 struct prop_location *wait_object;
487 struct selection_data *next;
490 /* Linked list of the above (in support of MULTIPLE targets). */
492 struct selection_data *converted_selections;
494 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
495 Atom conversion_fail_tag;
497 /* Used as an unwind-protect clause so that, if a selection-converter signals
498 an error, we tell the requester that we were unable to do what they wanted
499 before we throw to top-level or go into the debugger or whatever. */
501 static Lisp_Object
502 x_selection_request_lisp_error (Lisp_Object ignore)
504 struct selection_data *cs, *next;
506 for (cs = converted_selections; cs; cs = next)
508 next = cs->next;
509 if (cs->nofree == 0 && cs->data)
510 xfree (cs->data);
511 xfree (cs);
513 converted_selections = NULL;
515 if (x_selection_current_request != 0
516 && selection_request_dpyinfo->display)
517 x_decline_selection_request (x_selection_current_request);
518 return Qnil;
521 static Lisp_Object
522 x_catch_errors_unwind (Lisp_Object dummy)
524 BLOCK_INPUT;
525 x_uncatch_errors ();
526 UNBLOCK_INPUT;
527 return Qnil;
531 /* This stuff is so that INCR selections are reentrant (that is, so we can
532 be servicing multiple INCR selection requests simultaneously.) I haven't
533 actually tested that yet. */
535 /* Keep a list of the property changes that are awaited. */
537 struct prop_location
539 int identifier;
540 Display *display;
541 Window window;
542 Atom property;
543 int desired_state;
544 int arrived;
545 struct prop_location *next;
548 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
549 static void wait_for_property_change (struct prop_location *location);
550 static void unexpect_property_change (struct prop_location *location);
551 static int waiting_for_other_props_on_window (Display *display, Window window);
553 static int prop_location_identifier;
555 static Lisp_Object property_change_reply;
557 static struct prop_location *property_change_reply_object;
559 static struct prop_location *property_change_wait_list;
561 static Lisp_Object
562 queue_selection_requests_unwind (Lisp_Object tem)
564 x_stop_queuing_selection_requests ();
565 return Qnil;
569 /* Send the reply to a selection request event EVENT. */
571 #ifdef TRACE_SELECTION
572 static int x_reply_selection_request_cnt;
573 #endif /* TRACE_SELECTION */
575 static void
576 x_reply_selection_request (struct input_event *event, struct x_display_info *dpyinfo)
578 XEvent reply_base;
579 XSelectionEvent *reply = &(reply_base.xselection);
580 Display *display = SELECTION_EVENT_DISPLAY (event);
581 Window window = SELECTION_EVENT_REQUESTOR (event);
582 int bytes_remaining;
583 int max_bytes = SELECTION_QUANTUM (display);
584 int count = SPECPDL_INDEX ();
585 struct selection_data *cs;
587 if (max_bytes > MAX_SELECTION_QUANTUM)
588 max_bytes = MAX_SELECTION_QUANTUM;
590 reply->type = SelectionNotify;
591 reply->display = display;
592 reply->requestor = window;
593 reply->selection = SELECTION_EVENT_SELECTION (event);
594 reply->time = SELECTION_EVENT_TIME (event);
595 reply->target = SELECTION_EVENT_TARGET (event);
596 reply->property = SELECTION_EVENT_PROPERTY (event);
597 if (reply->property == None)
598 reply->property = reply->target;
600 BLOCK_INPUT;
601 /* The protected block contains wait_for_property_change, which can
602 run random lisp code (process handlers) or signal. Therefore, we
603 put the x_uncatch_errors call in an unwind. */
604 record_unwind_protect (x_catch_errors_unwind, Qnil);
605 x_catch_errors (display);
607 /* Loop over converted selections, storing them in the requested
608 properties. If data is large, only store the first N bytes
609 (section 2.7.2 of ICCCM). Note that we store the data for a
610 MULTIPLE request in the opposite order; the ICCM says only that
611 the conversion itself must be done in the same order. */
612 for (cs = converted_selections; cs; cs = cs->next)
614 if (cs->property == None)
615 continue;
617 bytes_remaining = cs->size * (cs->format / 8);
618 if (bytes_remaining <= max_bytes)
620 /* Send all the data at once, with minimal handshaking. */
621 TRACE1 ("Sending all %d bytes", bytes_remaining);
622 XChangeProperty (display, window, cs->property,
623 cs->type, cs->format, PropModeReplace,
624 cs->data, cs->size);
626 else
628 /* Send an INCR tag to initiate incremental transfer. */
629 long value[1];
631 TRACE2 ("Start sending %d bytes incrementally (%s)",
632 bytes_remaining, XGetAtomName (display, cs->property));
633 cs->wait_object
634 = expect_property_change (display, window, cs->property,
635 PropertyDelete);
637 /* XChangeProperty expects an array of long even if long is
638 more than 32 bits. */
639 value[0] = bytes_remaining;
640 XChangeProperty (display, window, cs->property,
641 dpyinfo->Xatom_INCR, 32, PropModeReplace,
642 (unsigned char *) value, 1);
643 XSelectInput (display, window, PropertyChangeMask);
647 /* Now issue the SelectionNotify event. */
648 XSendEvent (display, window, False, 0L, &reply_base);
649 XFlush (display);
651 #ifdef TRACE_SELECTION
653 char *sel = XGetAtomName (display, reply->selection);
654 char *tgt = XGetAtomName (display, reply->target);
655 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
656 sel, tgt, ++x_reply_selection_request_cnt);
657 if (sel) XFree (sel);
658 if (tgt) XFree (tgt);
660 #endif /* TRACE_SELECTION */
662 /* Finish sending the rest of each of the INCR values. This should
663 be improved; there's a chance of deadlock if more than one
664 subtarget in a MULTIPLE selection requires an INCR transfer, and
665 the requestor and Emacs loop waiting on different transfers. */
666 for (cs = converted_selections; cs; cs = cs->next)
667 if (cs->wait_object)
669 int format_bytes = cs->format / 8;
670 int had_errors = x_had_errors_p (display);
671 UNBLOCK_INPUT;
673 bytes_remaining = cs->size * format_bytes;
675 /* Wait for the requester to ack by deleting the property.
676 This can run Lisp code (process handlers) or signal. */
677 if (! had_errors)
679 TRACE1 ("Waiting for ACK (deletion of %s)",
680 XGetAtomName (display, cs->property));
681 wait_for_property_change (cs->wait_object);
683 else
684 unexpect_property_change (cs->wait_object);
686 while (bytes_remaining)
688 int i = ((bytes_remaining < max_bytes)
689 ? bytes_remaining
690 : max_bytes) / format_bytes;
691 BLOCK_INPUT;
693 cs->wait_object
694 = expect_property_change (display, window, cs->property,
695 PropertyDelete);
697 TRACE1 ("Sending increment of %d elements", i);
698 TRACE1 ("Set %s to increment data",
699 XGetAtomName (display, cs->property));
701 /* Append the next chunk of data to the property. */
702 XChangeProperty (display, window, cs->property,
703 cs->type, cs->format, PropModeAppend,
704 cs->data, i);
705 bytes_remaining -= i * format_bytes;
706 cs->data += i * ((cs->format == 32) ? sizeof (long)
707 : format_bytes);
708 XFlush (display);
709 had_errors = x_had_errors_p (display);
710 UNBLOCK_INPUT;
712 if (had_errors) break;
714 /* Wait for the requester to ack this chunk by deleting
715 the property. This can run Lisp code or signal. */
716 TRACE1 ("Waiting for increment ACK (deletion of %s)",
717 XGetAtomName (display, cs->property));
718 wait_for_property_change (cs->wait_object);
721 /* Now write a zero-length chunk to the property to tell the
722 requester that we're done. */
723 BLOCK_INPUT;
724 if (! waiting_for_other_props_on_window (display, window))
725 XSelectInput (display, window, 0L);
727 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
728 XGetAtomName (display, cs->property));
729 XChangeProperty (display, window, cs->property,
730 cs->type, cs->format, PropModeReplace,
731 cs->data, 0);
732 TRACE0 ("Done sending incrementally");
735 /* rms, 2003-01-03: I think I have fixed this bug. */
736 /* The window we're communicating with may have been deleted
737 in the meantime (that's a real situation from a bug report).
738 In this case, there may be events in the event queue still
739 refering to the deleted window, and we'll get a BadWindow error
740 in XTread_socket when processing the events. I don't have
741 an idea how to fix that. gerd, 2001-01-98. */
742 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
743 delivered before uncatch errors. */
744 XSync (display, False);
745 UNBLOCK_INPUT;
747 /* GTK queues events in addition to the queue in Xlib. So we
748 UNBLOCK to enter the event loop and get possible errors delivered,
749 and then BLOCK again because x_uncatch_errors requires it. */
750 BLOCK_INPUT;
751 /* This calls x_uncatch_errors. */
752 unbind_to (count, Qnil);
753 UNBLOCK_INPUT;
756 /* Handle a SelectionRequest event EVENT.
757 This is called from keyboard.c when such an event is found in the queue. */
759 static void
760 x_handle_selection_request (struct input_event *event)
762 struct gcpro gcpro1, gcpro2;
763 Time local_selection_time;
765 Display *display = SELECTION_EVENT_DISPLAY (event);
766 struct x_display_info *dpyinfo = x_display_info_for_display (display);
767 Atom selection = SELECTION_EVENT_SELECTION (event);
768 Lisp_Object selection_symbol = x_atom_to_symbol (display, selection);
769 Atom target = SELECTION_EVENT_TARGET (event);
770 Lisp_Object target_symbol = x_atom_to_symbol (display, target);
771 Atom property = SELECTION_EVENT_PROPERTY (event);
772 Lisp_Object local_selection_data;
773 int success = 0;
774 int count = SPECPDL_INDEX ();
775 GCPRO2 (local_selection_data, target_symbol);
777 if (!dpyinfo) goto DONE;
779 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
781 /* Decline if we don't own any selections. */
782 if (NILP (local_selection_data)) goto DONE;
784 /* Decline requests issued prior to our acquiring the selection. */
785 local_selection_time
786 = (Time) cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
787 if (SELECTION_EVENT_TIME (event) != CurrentTime
788 && local_selection_time > SELECTION_EVENT_TIME (event))
789 goto DONE;
791 x_selection_current_request = event;
792 selection_request_dpyinfo = dpyinfo;
793 record_unwind_protect (x_selection_request_lisp_error, Qnil);
795 /* We might be able to handle nested x_handle_selection_requests,
796 but this is difficult to test, and seems unimportant. */
797 x_start_queuing_selection_requests ();
798 record_unwind_protect (queue_selection_requests_unwind, Qnil);
800 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
801 SDATA (SYMBOL_NAME (selection_symbol)),
802 SDATA (SYMBOL_NAME (target_symbol)));
804 if (EQ (target_symbol, QMULTIPLE))
806 /* For MULTIPLE targets, the event property names a list of atom
807 pairs; the first atom names a target and the second names a
808 non-None property. */
809 Window requestor = SELECTION_EVENT_REQUESTOR (event);
810 Lisp_Object multprop;
811 int j, nselections;
813 if (property == None) goto DONE;
814 multprop
815 = x_get_window_property_as_lisp_data (display, requestor, property,
816 QMULTIPLE, selection);
818 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
819 goto DONE;
821 nselections = ASIZE (multprop) / 2;
822 /* Perform conversions. This can signal. */
823 for (j = 0; j < nselections; j++)
825 struct selection_data *cs = converted_selections + j;
826 Lisp_Object subtarget = AREF (multprop, 2*j);
827 Atom subproperty = symbol_to_x_atom (dpyinfo,
828 AREF (multprop, 2*j+1));
830 if (subproperty != None)
831 x_convert_selection (event, selection_symbol, subtarget,
832 subproperty, 1, dpyinfo);
834 success = 1;
836 else
838 if (property == None)
839 property = SELECTION_EVENT_TARGET (event);
840 success = x_convert_selection (event, selection_symbol,
841 target_symbol, property,
842 0, dpyinfo);
845 DONE:
847 if (success)
848 x_reply_selection_request (event, dpyinfo);
849 else
850 x_decline_selection_request (event);
851 x_selection_current_request = 0;
853 /* Run the `x-sent-selection-functions' abnormal hook. */
854 if (!NILP (Vx_sent_selection_functions)
855 && !EQ (Vx_sent_selection_functions, Qunbound))
857 Lisp_Object args[4];
858 args[0] = Vx_sent_selection_functions;
859 args[1] = selection_symbol;
860 args[2] = target_symbol;
861 args[3] = success ? Qt : Qnil;
862 Frun_hook_with_args (4, args);
865 unbind_to (count, Qnil);
866 UNGCPRO;
869 /* Perform the requested selection conversion, and write the data to
870 the converted_selections linked list, where it can be accessed by
871 x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out
872 the data even if conversion fails, using conversion_fail_tag.
874 Return 0 if the selection failed to convert, 1 otherwise. */
876 static int
877 x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
878 Lisp_Object target_symbol, Atom property,
879 int for_multiple, struct x_display_info *dpyinfo)
881 struct gcpro gcpro1;
882 Lisp_Object lisp_selection;
883 struct selection_data *cs;
884 GCPRO1 (lisp_selection);
886 lisp_selection
887 = x_get_local_selection (selection_symbol, target_symbol,
888 0, dpyinfo);
890 /* A nil return value means we can't perform the conversion. */
891 if (NILP (lisp_selection)
892 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
894 if (for_multiple)
896 cs = xmalloc (sizeof (struct selection_data));
897 cs->data = (unsigned char *) &conversion_fail_tag;
898 cs->size = 1;
899 cs->format = 32;
900 cs->type = XA_ATOM;
901 cs->nofree = 1;
902 cs->property = property;
903 cs->wait_object = NULL;
904 cs->next = converted_selections;
905 converted_selections = cs;
908 RETURN_UNGCPRO (0);
911 /* Otherwise, record the converted selection to binary. */
912 cs = xmalloc (sizeof (struct selection_data));
913 cs->nofree = 1;
914 cs->property = property;
915 cs->wait_object = NULL;
916 cs->next = converted_selections;
917 converted_selections = cs;
918 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
919 lisp_selection,
920 &(cs->data), &(cs->type),
921 &(cs->size), &(cs->format),
922 &(cs->nofree));
923 RETURN_UNGCPRO (1);
926 /* Handle a SelectionClear event EVENT, which indicates that some
927 client cleared out our previously asserted selection.
928 This is called from keyboard.c when such an event is found in the queue. */
930 static void
931 x_handle_selection_clear (struct input_event *event)
933 Display *display = SELECTION_EVENT_DISPLAY (event);
934 Atom selection = SELECTION_EVENT_SELECTION (event);
935 Time changed_owner_time = SELECTION_EVENT_TIME (event);
937 Lisp_Object selection_symbol, local_selection_data;
938 Time local_selection_time;
939 struct x_display_info *dpyinfo = x_display_info_for_display (display);
940 Lisp_Object Vselection_alist;
942 TRACE0 ("x_handle_selection_clear");
944 if (!dpyinfo) return;
946 selection_symbol = x_atom_to_symbol (display, selection);
947 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
949 /* Well, we already believe that we don't own it, so that's just fine. */
950 if (NILP (local_selection_data)) return;
952 local_selection_time = (Time)
953 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
955 /* We have reasserted the selection since this SelectionClear was
956 generated, so we can disregard it. */
957 if (changed_owner_time != CurrentTime
958 && local_selection_time > changed_owner_time)
959 return;
961 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
962 Vselection_alist = dpyinfo->terminal->Vselection_alist;
963 if (EQ (local_selection_data, CAR (Vselection_alist)))
964 Vselection_alist = XCDR (Vselection_alist);
965 else
967 Lisp_Object rest;
968 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
969 if (EQ (local_selection_data, CAR (XCDR (rest))))
971 XSETCDR (rest, XCDR (XCDR (rest)));
972 break;
975 dpyinfo->terminal->Vselection_alist = Vselection_alist;
977 /* Run the `x-lost-selection-functions' abnormal hook. */
979 Lisp_Object args[2];
980 args[0] = Vx_lost_selection_functions;
981 args[1] = selection_symbol;
982 Frun_hook_with_args (2, args);
985 prepare_menu_bars ();
986 redisplay_preserve_echo_area (20);
989 void
990 x_handle_selection_event (struct input_event *event)
992 TRACE0 ("x_handle_selection_event");
993 if (event->kind != SELECTION_REQUEST_EVENT)
994 x_handle_selection_clear (event);
995 else if (x_queue_selection_requests)
996 x_queue_event (event);
997 else
998 x_handle_selection_request (event);
1002 /* Clear all selections that were made from frame F.
1003 We do this when about to delete a frame. */
1005 void
1006 x_clear_frame_selections (FRAME_PTR f)
1008 Lisp_Object frame;
1009 Lisp_Object rest;
1010 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1011 struct terminal *t = dpyinfo->terminal;
1013 XSETFRAME (frame, f);
1015 /* Delete elements from the beginning of Vselection_alist. */
1016 while (CONSP (t->Vselection_alist)
1017 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
1019 /* Run the `x-lost-selection-functions' abnormal hook. */
1020 Lisp_Object args[2];
1021 args[0] = Vx_lost_selection_functions;
1022 args[1] = Fcar (Fcar (t->Vselection_alist));
1023 Frun_hook_with_args (2, args);
1025 t->Vselection_alist = XCDR (t->Vselection_alist);
1028 /* Delete elements after the beginning of Vselection_alist. */
1029 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
1030 if (CONSP (XCDR (rest))
1031 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
1033 Lisp_Object args[2];
1034 args[0] = Vx_lost_selection_functions;
1035 args[1] = XCAR (XCAR (XCDR (rest)));
1036 Frun_hook_with_args (2, args);
1037 XSETCDR (rest, XCDR (XCDR (rest)));
1038 break;
1042 /* Nonzero if any properties for DISPLAY and WINDOW
1043 are on the list of what we are waiting for. */
1045 static int
1046 waiting_for_other_props_on_window (Display *display, Window window)
1048 struct prop_location *rest = property_change_wait_list;
1049 while (rest)
1050 if (rest->display == display && rest->window == window)
1051 return 1;
1052 else
1053 rest = rest->next;
1054 return 0;
1057 /* Add an entry to the list of property changes we are waiting for.
1058 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1059 The return value is a number that uniquely identifies
1060 this awaited property change. */
1062 static struct prop_location *
1063 expect_property_change (Display *display, Window window, Atom property, int state)
1065 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1066 pl->identifier = ++prop_location_identifier;
1067 pl->display = display;
1068 pl->window = window;
1069 pl->property = property;
1070 pl->desired_state = state;
1071 pl->next = property_change_wait_list;
1072 pl->arrived = 0;
1073 property_change_wait_list = pl;
1074 return pl;
1077 /* Delete an entry from the list of property changes we are waiting for.
1078 IDENTIFIER is the number that uniquely identifies the entry. */
1080 static void
1081 unexpect_property_change (struct prop_location *location)
1083 struct prop_location *prev = 0, *rest = property_change_wait_list;
1084 while (rest)
1086 if (rest == location)
1088 if (prev)
1089 prev->next = rest->next;
1090 else
1091 property_change_wait_list = rest->next;
1092 xfree (rest);
1093 return;
1095 prev = rest;
1096 rest = rest->next;
1100 /* Remove the property change expectation element for IDENTIFIER. */
1102 static Lisp_Object
1103 wait_for_property_change_unwind (Lisp_Object loc)
1105 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1107 unexpect_property_change (location);
1108 if (location == property_change_reply_object)
1109 property_change_reply_object = 0;
1110 return Qnil;
1113 /* Actually wait for a property change.
1114 IDENTIFIER should be the value that expect_property_change returned. */
1116 static void
1117 wait_for_property_change (struct prop_location *location)
1119 int secs, usecs;
1120 int count = SPECPDL_INDEX ();
1122 if (property_change_reply_object)
1123 abort ();
1125 /* Make sure to do unexpect_property_change if we quit or err. */
1126 record_unwind_protect (wait_for_property_change_unwind,
1127 make_save_value (location, 0));
1129 XSETCAR (property_change_reply, Qnil);
1130 property_change_reply_object = location;
1132 /* If the event we are waiting for arrives beyond here, it will set
1133 property_change_reply, because property_change_reply_object says so. */
1134 if (! location->arrived)
1136 secs = x_selection_timeout / 1000;
1137 usecs = (x_selection_timeout % 1000) * 1000;
1138 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1139 wait_reading_process_output (secs, usecs, 0, 0,
1140 property_change_reply, NULL, 0);
1142 if (NILP (XCAR (property_change_reply)))
1144 TRACE0 (" Timed out");
1145 error ("Timed out waiting for property-notify event");
1149 unbind_to (count, Qnil);
1152 /* Called from XTread_socket in response to a PropertyNotify event. */
1154 void
1155 x_handle_property_notify (XPropertyEvent *event)
1157 struct prop_location *rest;
1159 for (rest = property_change_wait_list; rest; rest = rest->next)
1161 if (!rest->arrived
1162 && rest->property == event->atom
1163 && rest->window == event->window
1164 && rest->display == event->display
1165 && rest->desired_state == event->state)
1167 TRACE2 ("Expected %s of property %s",
1168 (event->state == PropertyDelete ? "deletion" : "change"),
1169 XGetAtomName (event->display, event->atom));
1171 rest->arrived = 1;
1173 /* If this is the one wait_for_property_change is waiting for,
1174 tell it to wake up. */
1175 if (rest == property_change_reply_object)
1176 XSETCAR (property_change_reply, Qt);
1178 return;
1185 /* Variables for communication with x_handle_selection_notify. */
1186 static Atom reading_which_selection;
1187 static Lisp_Object reading_selection_reply;
1188 static Window reading_selection_window;
1190 /* Do protocol to read selection-data from the server.
1191 Converts this to Lisp data and returns it.
1192 FRAME is the frame whose X window shall request the selection. */
1194 static Lisp_Object
1195 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1196 Lisp_Object time_stamp, Lisp_Object frame)
1198 struct frame *f = XFRAME (frame);
1199 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1200 Display *display = dpyinfo->display;
1201 Window requestor_window = FRAME_X_WINDOW (f);
1202 Time requestor_time = last_event_timestamp;
1203 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1204 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1205 Atom type_atom = (CONSP (target_type)
1206 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1207 : symbol_to_x_atom (dpyinfo, target_type));
1208 int secs, usecs;
1209 int count = SPECPDL_INDEX ();
1211 if (!FRAME_LIVE_P (f))
1212 return Qnil;
1214 if (! NILP (time_stamp))
1216 if (CONSP (time_stamp))
1217 requestor_time = (Time) cons_to_long (time_stamp);
1218 else if (INTEGERP (time_stamp))
1219 requestor_time = (Time) XUINT (time_stamp);
1220 else if (FLOATP (time_stamp))
1221 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1222 else
1223 error ("TIME_STAMP must be cons or number");
1226 BLOCK_INPUT;
1228 /* The protected block contains wait_reading_process_output, which
1229 can run random lisp code (process handlers) or signal.
1230 Therefore, we put the x_uncatch_errors call in an unwind. */
1231 record_unwind_protect (x_catch_errors_unwind, Qnil);
1232 x_catch_errors (display);
1234 TRACE2 ("Get selection %s, type %s",
1235 XGetAtomName (display, type_atom),
1236 XGetAtomName (display, target_property));
1238 XConvertSelection (display, selection_atom, type_atom, target_property,
1239 requestor_window, requestor_time);
1240 XFlush (display);
1242 /* Prepare to block until the reply has been read. */
1243 reading_selection_window = requestor_window;
1244 reading_which_selection = selection_atom;
1245 XSETCAR (reading_selection_reply, Qnil);
1247 /* It should not be necessary to stop handling selection requests
1248 during this time. In fact, the SAVE_TARGETS mechanism requires
1249 us to handle a clipboard manager's requests before it returns
1250 SelectionNotify. */
1251 #if 0
1252 x_start_queuing_selection_requests ();
1253 record_unwind_protect (queue_selection_requests_unwind, Qnil);
1254 #endif
1256 UNBLOCK_INPUT;
1258 /* This allows quits. Also, don't wait forever. */
1259 secs = x_selection_timeout / 1000;
1260 usecs = (x_selection_timeout % 1000) * 1000;
1261 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1262 wait_reading_process_output (secs, usecs, 0, 0,
1263 reading_selection_reply, NULL, 0);
1264 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1266 BLOCK_INPUT;
1267 if (x_had_errors_p (display))
1268 error ("Cannot get selection");
1269 /* This calls x_uncatch_errors. */
1270 unbind_to (count, Qnil);
1271 UNBLOCK_INPUT;
1273 if (NILP (XCAR (reading_selection_reply)))
1274 error ("Timed out waiting for reply from selection owner");
1275 if (EQ (XCAR (reading_selection_reply), Qlambda))
1276 return Qnil;
1278 /* Otherwise, the selection is waiting for us on the requested property. */
1279 return
1280 x_get_window_property_as_lisp_data (display, requestor_window,
1281 target_property, target_type,
1282 selection_atom);
1285 /* Subroutines of x_get_window_property_as_lisp_data */
1287 /* Use xfree, not XFree, to free the data obtained with this function. */
1289 static void
1290 x_get_window_property (Display *display, Window window, Atom property,
1291 unsigned char **data_ret, int *bytes_ret,
1292 Atom *actual_type_ret, int *actual_format_ret,
1293 unsigned long *actual_size_ret, int delete_p)
1295 int total_size;
1296 unsigned long bytes_remaining;
1297 int offset = 0;
1298 unsigned char *tmp_data = 0;
1299 int result;
1300 int buffer_size = SELECTION_QUANTUM (display);
1302 if (buffer_size > MAX_SELECTION_QUANTUM)
1303 buffer_size = MAX_SELECTION_QUANTUM;
1305 BLOCK_INPUT;
1307 /* First probe the thing to find out how big it is. */
1308 result = XGetWindowProperty (display, window, property,
1309 0L, 0L, False, AnyPropertyType,
1310 actual_type_ret, actual_format_ret,
1311 actual_size_ret,
1312 &bytes_remaining, &tmp_data);
1313 if (result != Success)
1315 UNBLOCK_INPUT;
1316 *data_ret = 0;
1317 *bytes_ret = 0;
1318 return;
1321 /* This was allocated by Xlib, so use XFree. */
1322 XFree ((char *) tmp_data);
1324 if (*actual_type_ret == None || *actual_format_ret == 0)
1326 UNBLOCK_INPUT;
1327 return;
1330 total_size = bytes_remaining + 1;
1331 *data_ret = (unsigned char *) xmalloc (total_size);
1333 /* Now read, until we've gotten it all. */
1334 while (bytes_remaining)
1336 #ifdef TRACE_SELECTION
1337 unsigned long last = bytes_remaining;
1338 #endif
1339 result
1340 = XGetWindowProperty (display, window, property,
1341 (long)offset/4, (long)buffer_size/4,
1342 False,
1343 AnyPropertyType,
1344 actual_type_ret, actual_format_ret,
1345 actual_size_ret, &bytes_remaining, &tmp_data);
1347 TRACE2 ("Read %lu bytes from property %s",
1348 last - bytes_remaining,
1349 XGetAtomName (display, property));
1351 /* If this doesn't return Success at this point, it means that
1352 some clod deleted the selection while we were in the midst of
1353 reading it. Deal with that, I guess.... */
1354 if (result != Success)
1355 break;
1357 /* The man page for XGetWindowProperty says:
1358 "If the returned format is 32, the returned data is represented
1359 as a long array and should be cast to that type to obtain the
1360 elements."
1361 This applies even if long is more than 32 bits, the X library
1362 converts from 32 bit elements received from the X server to long
1363 and passes the long array to us. Thus, for that case memcpy can not
1364 be used. We convert to a 32 bit type here, because so much code
1365 assume on that.
1367 The bytes and offsets passed to XGetWindowProperty refers to the
1368 property and those are indeed in 32 bit quantities if format is 32. */
1370 if (32 < BITS_PER_LONG && *actual_format_ret == 32)
1372 unsigned long i;
1373 int *idata = (int *) ((*data_ret) + offset);
1374 long *ldata = (long *) tmp_data;
1376 for (i = 0; i < *actual_size_ret; ++i)
1378 idata[i]= (int) ldata[i];
1379 offset += 4;
1382 else
1384 *actual_size_ret *= *actual_format_ret / 8;
1385 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1386 offset += *actual_size_ret;
1389 /* This was allocated by Xlib, so use XFree. */
1390 XFree ((char *) tmp_data);
1393 XFlush (display);
1394 UNBLOCK_INPUT;
1395 *bytes_ret = offset;
1398 /* Use xfree, not XFree, to free the data obtained with this function. */
1400 static void
1401 receive_incremental_selection (Display *display, Window window, Atom property,
1402 Lisp_Object target_type,
1403 unsigned int min_size_bytes,
1404 unsigned char **data_ret, int *size_bytes_ret,
1405 Atom *type_ret, int *format_ret,
1406 unsigned long *size_ret)
1408 int offset = 0;
1409 struct prop_location *wait_object;
1410 *size_bytes_ret = min_size_bytes;
1411 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1413 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1415 /* At this point, we have read an INCR property.
1416 Delete the property to ack it.
1417 (But first, prepare to receive the next event in this handshake.)
1419 Now, we must loop, waiting for the sending window to put a value on
1420 that property, then reading the property, then deleting it to ack.
1421 We are done when the sender places a property of length 0.
1423 BLOCK_INPUT;
1424 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1425 TRACE1 (" Delete property %s",
1426 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1427 XDeleteProperty (display, window, property);
1428 TRACE1 (" Expect new value of property %s",
1429 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1430 wait_object = expect_property_change (display, window, property,
1431 PropertyNewValue);
1432 XFlush (display);
1433 UNBLOCK_INPUT;
1435 while (1)
1437 unsigned char *tmp_data;
1438 int tmp_size_bytes;
1440 TRACE0 (" Wait for property change");
1441 wait_for_property_change (wait_object);
1443 /* expect it again immediately, because x_get_window_property may
1444 .. no it won't, I don't get it.
1445 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1446 TRACE0 (" Get property value");
1447 x_get_window_property (display, window, property,
1448 &tmp_data, &tmp_size_bytes,
1449 type_ret, format_ret, size_ret, 1);
1451 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1453 if (tmp_size_bytes == 0) /* we're done */
1455 TRACE0 ("Done reading incrementally");
1457 if (! waiting_for_other_props_on_window (display, window))
1458 XSelectInput (display, window, STANDARD_EVENT_SET);
1459 /* Use xfree, not XFree, because x_get_window_property
1460 calls xmalloc itself. */
1461 xfree (tmp_data);
1462 break;
1465 BLOCK_INPUT;
1466 TRACE1 (" ACK by deleting property %s",
1467 XGetAtomName (display, property));
1468 XDeleteProperty (display, window, property);
1469 wait_object = expect_property_change (display, window, property,
1470 PropertyNewValue);
1471 XFlush (display);
1472 UNBLOCK_INPUT;
1474 if (*size_bytes_ret < offset + tmp_size_bytes)
1476 *size_bytes_ret = offset + tmp_size_bytes;
1477 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1480 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1481 offset += tmp_size_bytes;
1483 /* Use xfree, not XFree, because x_get_window_property
1484 calls xmalloc itself. */
1485 xfree (tmp_data);
1490 /* Fetch a value from property PROPERTY of X window WINDOW on display
1491 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1492 if this fails. */
1494 static Lisp_Object
1495 x_get_window_property_as_lisp_data (Display *display, Window window,
1496 Atom property,
1497 Lisp_Object target_type,
1498 Atom selection_atom)
1500 Atom actual_type;
1501 int actual_format;
1502 unsigned long actual_size;
1503 unsigned char *data = 0;
1504 int bytes = 0;
1505 Lisp_Object val;
1506 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1508 TRACE0 ("Reading selection data");
1510 x_get_window_property (display, window, property, &data, &bytes,
1511 &actual_type, &actual_format, &actual_size, 1);
1512 if (! data)
1514 int there_is_a_selection_owner;
1515 BLOCK_INPUT;
1516 there_is_a_selection_owner
1517 = XGetSelectionOwner (display, selection_atom);
1518 UNBLOCK_INPUT;
1519 if (there_is_a_selection_owner)
1520 signal_error ("Selection owner couldn't convert",
1521 actual_type
1522 ? list2 (target_type,
1523 x_atom_to_symbol (display, actual_type))
1524 : target_type);
1525 else
1526 signal_error ("No selection",
1527 x_atom_to_symbol (display, selection_atom));
1530 if (actual_type == dpyinfo->Xatom_INCR)
1532 /* That wasn't really the data, just the beginning. */
1534 unsigned int min_size_bytes = * ((unsigned int *) data);
1535 BLOCK_INPUT;
1536 /* Use xfree, not XFree, because x_get_window_property
1537 calls xmalloc itself. */
1538 xfree ((char *) data);
1539 UNBLOCK_INPUT;
1540 receive_incremental_selection (display, window, property, target_type,
1541 min_size_bytes, &data, &bytes,
1542 &actual_type, &actual_format,
1543 &actual_size);
1546 BLOCK_INPUT;
1547 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1548 XDeleteProperty (display, window, property);
1549 XFlush (display);
1550 UNBLOCK_INPUT;
1552 /* It's been read. Now convert it to a lisp object in some semi-rational
1553 manner. */
1554 val = selection_data_to_lisp_data (display, data, bytes,
1555 actual_type, actual_format);
1557 /* Use xfree, not XFree, because x_get_window_property
1558 calls xmalloc itself. */
1559 xfree ((char *) data);
1560 return val;
1563 /* These functions convert from the selection data read from the server into
1564 something that we can use from Lisp, and vice versa.
1566 Type: Format: Size: Lisp Type:
1567 ----- ------- ----- -----------
1568 * 8 * String
1569 ATOM 32 1 Symbol
1570 ATOM 32 > 1 Vector of Symbols
1571 * 16 1 Integer
1572 * 16 > 1 Vector of Integers
1573 * 32 1 if <=16 bits: Integer
1574 if > 16 bits: Cons of top16, bot16
1575 * 32 > 1 Vector of the above
1577 When converting a Lisp number to C, it is assumed to be of format 16 if
1578 it is an integer, and of format 32 if it is a cons of two integers.
1580 When converting a vector of numbers from Lisp to C, it is assumed to be
1581 of format 16 if every element in the vector is an integer, and is assumed
1582 to be of format 32 if any element is a cons of two integers.
1584 When converting an object to C, it may be of the form (SYMBOL . <data>)
1585 where SYMBOL is what we should claim that the type is. Format and
1586 representation are as above.
1588 Important: When format is 32, data should contain an array of int,
1589 not an array of long as the X library returns. This makes a difference
1590 when sizeof(long) != sizeof(int). */
1594 static Lisp_Object
1595 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1596 int size, Atom type, int format)
1598 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1600 if (type == dpyinfo->Xatom_NULL)
1601 return QNULL;
1603 /* Convert any 8-bit data to a string, for compactness. */
1604 else if (format == 8)
1606 Lisp_Object str, lispy_type;
1608 str = make_unibyte_string ((char *) data, size);
1609 /* Indicate that this string is from foreign selection by a text
1610 property `foreign-selection' so that the caller of
1611 x-get-selection-internal (usually x-get-selection) can know
1612 that the string must be decode. */
1613 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1614 lispy_type = QCOMPOUND_TEXT;
1615 else if (type == dpyinfo->Xatom_UTF8_STRING)
1616 lispy_type = QUTF8_STRING;
1617 else
1618 lispy_type = QSTRING;
1619 Fput_text_property (make_number (0), make_number (size),
1620 Qforeign_selection, lispy_type, str);
1621 return str;
1623 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1624 a vector of symbols. */
1625 else if (type == XA_ATOM
1626 /* Treat ATOM_PAIR type similar to list of atoms. */
1627 || type == dpyinfo->Xatom_ATOM_PAIR)
1629 int i;
1630 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1631 But the callers of these function has made sure the data for
1632 format == 32 is an array of int. Thus, use int instead
1633 of Atom. */
1634 int *idata = (int *) data;
1636 if (size == sizeof (int))
1637 return x_atom_to_symbol (display, (Atom) idata[0]);
1638 else
1640 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1641 make_number (0));
1642 for (i = 0; i < size / sizeof (int); i++)
1643 Faset (v, make_number (i),
1644 x_atom_to_symbol (display, (Atom) idata[i]));
1645 return v;
1649 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1650 If the number is 32 bits and won't fit in a Lisp_Int,
1651 convert it to a cons of integers, 16 bits in each half.
1653 else if (format == 32 && size == sizeof (int))
1654 return long_to_cons (((unsigned int *) data) [0]);
1655 else if (format == 16 && size == sizeof (short))
1656 return make_number ((int) (((unsigned short *) data) [0]));
1658 /* Convert any other kind of data to a vector of numbers, represented
1659 as above (as an integer, or a cons of two 16 bit integers.)
1661 else if (format == 16)
1663 int i;
1664 Lisp_Object v;
1665 v = Fmake_vector (make_number (size / 2), make_number (0));
1666 for (i = 0; i < size / 2; i++)
1668 int j = (int) ((unsigned short *) data) [i];
1669 Faset (v, make_number (i), make_number (j));
1671 return v;
1673 else
1675 int i;
1676 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1677 for (i = 0; i < size / 4; i++)
1679 unsigned int j = ((unsigned int *) data) [i];
1680 Faset (v, make_number (i), long_to_cons (j));
1682 return v;
1687 /* Use xfree, not XFree, to free the data obtained with this function. */
1689 static void
1690 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1691 unsigned char **data_ret, Atom *type_ret,
1692 unsigned int *size_ret,
1693 int *format_ret, int *nofree_ret)
1695 Lisp_Object type = Qnil;
1696 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1698 *nofree_ret = 0;
1700 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1702 type = XCAR (obj);
1703 obj = XCDR (obj);
1704 if (CONSP (obj) && NILP (XCDR (obj)))
1705 obj = XCAR (obj);
1708 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1709 { /* This is not the same as declining */
1710 *format_ret = 32;
1711 *size_ret = 0;
1712 *data_ret = 0;
1713 type = QNULL;
1715 else if (STRINGP (obj))
1717 if (SCHARS (obj) < SBYTES (obj))
1718 /* OBJ is a multibyte string containing a non-ASCII char. */
1719 signal_error ("Non-ASCII string must be encoded in advance", obj);
1720 if (NILP (type))
1721 type = QSTRING;
1722 *format_ret = 8;
1723 *size_ret = SBYTES (obj);
1724 *data_ret = SDATA (obj);
1725 *nofree_ret = 1;
1727 else if (SYMBOLP (obj))
1729 *format_ret = 32;
1730 *size_ret = 1;
1731 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1732 (*data_ret) [sizeof (Atom)] = 0;
1733 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, obj);
1734 if (NILP (type)) type = QATOM;
1736 else if (INTEGERP (obj)
1737 && XINT (obj) < 0xFFFF
1738 && XINT (obj) > -0xFFFF)
1740 *format_ret = 16;
1741 *size_ret = 1;
1742 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1743 (*data_ret) [sizeof (short)] = 0;
1744 (*(short **) data_ret) [0] = (short) XINT (obj);
1745 if (NILP (type)) type = QINTEGER;
1747 else if (INTEGERP (obj)
1748 || (CONSP (obj) && INTEGERP (XCAR (obj))
1749 && (INTEGERP (XCDR (obj))
1750 || (CONSP (XCDR (obj))
1751 && INTEGERP (XCAR (XCDR (obj)))))))
1753 *format_ret = 32;
1754 *size_ret = 1;
1755 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1756 (*data_ret) [sizeof (long)] = 0;
1757 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1758 if (NILP (type)) type = QINTEGER;
1760 else if (VECTORP (obj))
1762 /* Lisp_Vectors may represent a set of ATOMs;
1763 a set of 16 or 32 bit INTEGERs;
1764 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1766 int i;
1768 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1769 /* This vector is an ATOM set */
1771 if (NILP (type)) type = QATOM;
1772 *size_ret = ASIZE (obj);
1773 *format_ret = 32;
1774 for (i = 0; i < *size_ret; i++)
1775 if (!SYMBOLP (XVECTOR (obj)->contents [i]))
1776 signal_error ("All elements of selection vector must have same type", obj);
1778 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1779 for (i = 0; i < *size_ret; i++)
1780 (*(Atom **) data_ret) [i]
1781 = symbol_to_x_atom (dpyinfo, XVECTOR (obj)->contents [i]);
1783 else
1784 /* This vector is an INTEGER set, or something like it */
1786 int data_size = 2;
1787 *size_ret = ASIZE (obj);
1788 if (NILP (type)) type = QINTEGER;
1789 *format_ret = 16;
1790 for (i = 0; i < *size_ret; i++)
1791 if (CONSP (XVECTOR (obj)->contents [i]))
1792 *format_ret = 32;
1793 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1794 signal_error (/* Qselection_error */
1795 "Elements of selection vector must be integers or conses of integers",
1796 obj);
1798 /* Use sizeof(long) even if it is more than 32 bits. See comment
1799 in x_get_window_property and x_fill_property_data. */
1801 if (*format_ret == 32) data_size = sizeof(long);
1802 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1803 for (i = 0; i < *size_ret; i++)
1804 if (*format_ret == 32)
1805 (*((unsigned long **) data_ret)) [i]
1806 = cons_to_long (XVECTOR (obj)->contents [i]);
1807 else
1808 (*((unsigned short **) data_ret)) [i]
1809 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1812 else
1813 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1815 *type_ret = symbol_to_x_atom (dpyinfo, type);
1818 static Lisp_Object
1819 clean_local_selection_data (Lisp_Object obj)
1821 if (CONSP (obj)
1822 && INTEGERP (XCAR (obj))
1823 && CONSP (XCDR (obj))
1824 && INTEGERP (XCAR (XCDR (obj)))
1825 && NILP (XCDR (XCDR (obj))))
1826 obj = Fcons (XCAR (obj), XCDR (obj));
1828 if (CONSP (obj)
1829 && INTEGERP (XCAR (obj))
1830 && INTEGERP (XCDR (obj)))
1832 if (XINT (XCAR (obj)) == 0)
1833 return XCDR (obj);
1834 if (XINT (XCAR (obj)) == -1)
1835 return make_number (- XINT (XCDR (obj)));
1837 if (VECTORP (obj))
1839 int i;
1840 int size = ASIZE (obj);
1841 Lisp_Object copy;
1842 if (size == 1)
1843 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1844 copy = Fmake_vector (make_number (size), Qnil);
1845 for (i = 0; i < size; i++)
1846 XVECTOR (copy)->contents [i]
1847 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1848 return copy;
1850 return obj;
1853 /* Called from XTread_socket to handle SelectionNotify events.
1854 If it's the selection we are waiting for, stop waiting
1855 by setting the car of reading_selection_reply to non-nil.
1856 We store t there if the reply is successful, lambda if not. */
1858 void
1859 x_handle_selection_notify (XSelectionEvent *event)
1861 if (event->requestor != reading_selection_window)
1862 return;
1863 if (event->selection != reading_which_selection)
1864 return;
1866 TRACE0 ("Received SelectionNotify");
1867 XSETCAR (reading_selection_reply,
1868 (event->property != 0 ? Qt : Qlambda));
1872 /* From a Lisp_Object, return a suitable frame for selection
1873 operations. OBJECT may be a frame, a terminal object, or nil
1874 (which stands for the selected frame--or, if that is not an X
1875 frame, the first X display on the list). If no suitable frame can
1876 be found, return NULL. */
1878 static struct frame *
1879 frame_for_x_selection (Lisp_Object object)
1881 Lisp_Object tail, frame;
1882 struct frame *f;
1884 if (NILP (object))
1886 f = XFRAME (selected_frame);
1887 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1888 return f;
1890 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1892 f = XFRAME (XCAR (tail));
1893 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1894 return f;
1897 else if (TERMINALP (object))
1899 struct terminal *t = get_terminal (object, 1);
1900 if (t->type == output_x_window)
1902 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1904 f = XFRAME (XCAR (tail));
1905 if (FRAME_LIVE_P (f) && f->terminal == t)
1906 return f;
1910 else if (FRAMEP (object))
1912 f = XFRAME (object);
1913 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1914 return f;
1917 return NULL;
1921 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1922 Sx_own_selection_internal, 2, 3, 0,
1923 doc: /* Assert an X selection of type SELECTION and value VALUE.
1924 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1925 \(Those are literal upper-case symbol names, since that's what X expects.)
1926 VALUE is typically a string, or a cons of two markers, but may be
1927 anything that the functions on `selection-converter-alist' know about.
1929 FRAME should be a frame that should own the selection. If omitted or
1930 nil, it defaults to the selected frame. */)
1931 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1933 if (NILP (frame)) frame = selected_frame;
1934 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1935 error ("X selection unavailable for this frame");
1937 CHECK_SYMBOL (selection);
1938 if (NILP (value)) error ("VALUE may not be nil");
1939 x_own_selection (selection, value, frame);
1940 return value;
1944 /* Request the selection value from the owner. If we are the owner,
1945 simply return our selection value. If we are not the owner, this
1946 will block until all of the data has arrived. */
1948 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1949 Sx_get_selection_internal, 2, 4, 0,
1950 doc: /* Return text selected from some X window.
1951 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1952 \(Those are literal upper-case symbol names, since that's what X expects.)
1953 TYPE is the type of data desired, typically `STRING'.
1954 TIME_STAMP is the time to use in the XConvertSelection call for foreign
1955 selections. If omitted, defaults to the time for the last event.
1957 TERMINAL should be a terminal object or a frame specifying the X
1958 server to query. If omitted or nil, that stands for the selected
1959 frame's display, or the first available X display. */)
1960 (Lisp_Object selection_symbol, Lisp_Object target_type,
1961 Lisp_Object time_stamp, Lisp_Object terminal)
1963 Lisp_Object val = Qnil;
1964 struct gcpro gcpro1, gcpro2;
1965 struct frame *f = frame_for_x_selection (terminal);
1966 GCPRO2 (target_type, val); /* we store newly consed data into these */
1968 CHECK_SYMBOL (selection_symbol);
1969 CHECK_SYMBOL (target_type);
1970 if (EQ (target_type, QMULTIPLE))
1971 error ("Retrieving MULTIPLE selections is currently unimplemented");
1972 if (!f)
1973 error ("X selection unavailable for this frame");
1975 val = x_get_local_selection (selection_symbol, target_type, 1,
1976 FRAME_X_DISPLAY_INFO (f));
1978 if (NILP (val) && FRAME_LIVE_P (f))
1980 Lisp_Object frame;
1981 XSETFRAME (frame, f);
1982 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1983 time_stamp, frame));
1986 if (CONSP (val) && SYMBOLP (XCAR (val)))
1988 val = XCDR (val);
1989 if (CONSP (val) && NILP (XCDR (val)))
1990 val = XCAR (val);
1992 RETURN_UNGCPRO (clean_local_selection_data (val));
1995 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
1996 Sx_disown_selection_internal, 1, 3, 0,
1997 doc: /* If we own the selection SELECTION, disown it.
1998 Disowning it means there is no such selection.
2000 TERMINAL should be a terminal object or a frame specifying the X
2001 server to query. If omitted or nil, that stands for the selected
2002 frame's display, or the first available X display. */)
2003 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
2005 Time timestamp;
2006 Atom selection_atom;
2007 union {
2008 struct selection_input_event sie;
2009 struct input_event ie;
2010 } event;
2011 struct frame *f = frame_for_x_selection (terminal);
2012 struct x_display_info *dpyinfo;
2014 if (!f)
2015 return Qnil;
2017 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2018 CHECK_SYMBOL (selection);
2020 /* Don't disown the selection when we're not the owner. */
2021 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2022 return Qnil;
2024 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2026 BLOCK_INPUT;
2027 timestamp = (NILP (time_object) ? last_event_timestamp
2028 : cons_to_long (time_object));
2029 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2030 UNBLOCK_INPUT;
2032 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2033 generated for a window which owns the selection when that window sets
2034 the selection owner to None. The NCD server does, the MIT Sun4 server
2035 doesn't. So we synthesize one; this means we might get two, but
2036 that's ok, because the second one won't have any effect. */
2037 SELECTION_EVENT_DISPLAY (&event.sie) = dpyinfo->display;
2038 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2039 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2040 x_handle_selection_clear (&event.ie);
2042 return Qt;
2045 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2046 0, 2, 0,
2047 doc: /* Whether the current Emacs process owns the given X Selection.
2048 The arg should be the name of the selection in question, typically one of
2049 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2050 \(Those are literal upper-case symbol names, since that's what X expects.)
2051 For convenience, the symbol nil is the same as `PRIMARY',
2052 and t is the same as `SECONDARY'.
2054 TERMINAL should be a terminal object or a frame specifying the X
2055 server to query. If omitted or nil, that stands for the selected
2056 frame's display, or the first available X display. */)
2057 (Lisp_Object selection, Lisp_Object terminal)
2059 struct frame *f = frame_for_x_selection (terminal);
2061 CHECK_SYMBOL (selection);
2062 if (EQ (selection, Qnil)) selection = QPRIMARY;
2063 if (EQ (selection, Qt)) selection = QSECONDARY;
2065 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f))))
2066 return Qt;
2067 else
2068 return Qnil;
2071 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2072 0, 2, 0,
2073 doc: /* Whether there is an owner for the given X selection.
2074 SELECTION should be the name of the selection in question, typically
2075 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
2076 these literal upper-case names.) The symbol nil is the same as
2077 `PRIMARY', and t is the same as `SECONDARY'.
2079 TERMINAL should be a terminal object or a frame specifying the X
2080 server to query. If omitted or nil, that stands for the selected
2081 frame's display, or the first available X display. */)
2082 (Lisp_Object selection, Lisp_Object terminal)
2084 Window owner;
2085 Atom atom;
2086 struct frame *f = frame_for_x_selection (terminal);
2087 struct x_display_info *dpyinfo;
2089 CHECK_SYMBOL (selection);
2090 if (EQ (selection, Qnil)) selection = QPRIMARY;
2091 if (EQ (selection, Qt)) selection = QSECONDARY;
2093 if (!f)
2094 return Qnil;
2096 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2098 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2099 return Qt;
2101 atom = symbol_to_x_atom (dpyinfo, selection);
2102 if (atom == 0) return Qnil;
2103 BLOCK_INPUT;
2104 owner = XGetSelectionOwner (dpyinfo->display, atom);
2105 UNBLOCK_INPUT;
2106 return (owner ? Qt : Qnil);
2109 /* Send the clipboard manager a SAVE_TARGETS request with a
2110 UTF8_STRING property, as described by
2111 http://www.freedesktop.org/wiki/ClipboardManager */
2113 void
2114 x_clipboard_manager_save (struct x_display_info *dpyinfo,
2115 Lisp_Object frame)
2117 struct frame *f = XFRAME (frame);
2118 Atom data = dpyinfo->Xatom_UTF8_STRING;
2120 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2121 dpyinfo->Xatom_EMACS_TMP,
2122 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2123 (unsigned char *) &data, 1);
2124 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2125 Qnil, frame);
2128 DEFUN ("x-clipboard-manager-save", Fx_clipboard_manager_save,
2129 Sx_clipboard_manager_save, 0, 1, 0,
2130 doc: /* Save the clipboard contents to the clipboard manager.
2131 This function is intended to run from `delete-frame-functions' and
2132 `kill-emacs-hook', to transfer clipboard data owned by Emacs to a
2133 clipboard manager prior to deleting a frame or killing Emacs.
2135 FRAME specifies a frame owning a clipboard; do nothing if FRAME does
2136 not own the clipboard, or if no clipboard manager is present. If
2137 FRAME is nil, save all clipboard contents owned by Emacs. */)
2138 (Lisp_Object frame)
2140 if (FRAMEP (frame))
2142 struct frame *f = XFRAME (frame);
2143 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
2145 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2146 Lisp_Object local_selection
2147 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2149 if (!NILP (local_selection)
2150 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2151 && XGetSelectionOwner (dpyinfo->display,
2152 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2153 x_clipboard_manager_save (dpyinfo, frame);
2156 else if (NILP (frame))
2158 /* Loop through all X displays, saving owned clipboards. */
2159 struct x_display_info *dpyinfo;
2160 Lisp_Object local_selection, frame;
2161 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2163 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2164 if (NILP (local_selection)
2165 || !XGetSelectionOwner (dpyinfo->display,
2166 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2167 continue;
2169 frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2170 if (FRAME_LIVE_P (XFRAME (frame)))
2171 x_clipboard_manager_save (dpyinfo, frame);
2175 return Qnil;
2179 /***********************************************************************
2180 Drag and drop support
2181 ***********************************************************************/
2182 /* Check that lisp values are of correct type for x_fill_property_data.
2183 That is, number, string or a cons with two numbers (low and high 16
2184 bit parts of a 32 bit number). Return the number of items in DATA,
2185 or -1 if there is an error. */
2188 x_check_property_data (Lisp_Object data)
2190 Lisp_Object iter;
2191 int size = 0;
2193 for (iter = data; CONSP (iter); iter = XCDR (iter))
2195 Lisp_Object o = XCAR (iter);
2197 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2198 return -1;
2199 else if (CONSP (o) &&
2200 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2201 return -1;
2202 size++;
2205 return size;
2208 /* Convert lisp values to a C array. Values may be a number, a string
2209 which is taken as an X atom name and converted to the atom value, or
2210 a cons containing the two 16 bit parts of a 32 bit number.
2212 DPY is the display use to look up X atoms.
2213 DATA is a Lisp list of values to be converted.
2214 RET is the C array that contains the converted values. It is assumed
2215 it is big enough to hold all values.
2216 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2217 be stored in RET. Note that long is used for 32 even if long is more
2218 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2219 XClientMessageEvent). */
2221 void
2222 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2224 long val;
2225 long *d32 = (long *) ret;
2226 short *d16 = (short *) ret;
2227 char *d08 = (char *) ret;
2228 Lisp_Object iter;
2230 for (iter = data; CONSP (iter); iter = XCDR (iter))
2232 Lisp_Object o = XCAR (iter);
2234 if (INTEGERP (o))
2235 val = (long) XFASTINT (o);
2236 else if (FLOATP (o))
2237 val = (long) XFLOAT_DATA (o);
2238 else if (CONSP (o))
2239 val = (long) cons_to_long (o);
2240 else if (STRINGP (o))
2242 BLOCK_INPUT;
2243 val = (long) XInternAtom (dpy, SSDATA (o), False);
2244 UNBLOCK_INPUT;
2246 else
2247 error ("Wrong type, must be string, number or cons");
2249 if (format == 8)
2250 *d08++ = (char) val;
2251 else if (format == 16)
2252 *d16++ = (short) val;
2253 else
2254 *d32++ = val;
2258 /* Convert an array of C values to a Lisp list.
2259 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2260 DATA is a C array of values to be converted.
2261 TYPE is the type of the data. Only XA_ATOM is special, it converts
2262 each number in DATA to its corresponfing X atom as a symbol.
2263 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2264 be stored in RET.
2265 SIZE is the number of elements in DATA.
2267 Important: When format is 32, data should contain an array of int,
2268 not an array of long as the X library returns. This makes a difference
2269 when sizeof(long) != sizeof(int).
2271 Also see comment for selection_data_to_lisp_data above. */
2273 Lisp_Object
2274 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2275 Atom type, int format, long unsigned int size)
2277 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2278 data, size*format/8, type, format);
2281 /* Get the mouse position in frame relative coordinates. */
2283 static void
2284 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2286 Window root, dummy_window;
2287 int dummy;
2289 BLOCK_INPUT;
2291 XQueryPointer (FRAME_X_DISPLAY (f),
2292 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2294 /* The root window which contains the pointer. */
2295 &root,
2297 /* Window pointer is on, not used */
2298 &dummy_window,
2300 /* The position on that root window. */
2301 x, y,
2303 /* x/y in dummy_window coordinates, not used. */
2304 &dummy, &dummy,
2306 /* Modifier keys and pointer buttons, about which
2307 we don't care. */
2308 (unsigned int *) &dummy);
2311 /* Absolute to relative. */
2312 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2313 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2315 UNBLOCK_INPUT;
2318 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2319 Sx_get_atom_name, 1, 2, 0,
2320 doc: /* Return the X atom name for VALUE as a string.
2321 VALUE may be a number or a cons where the car is the upper 16 bits and
2322 the cdr is the lower 16 bits of a 32 bit value.
2323 Use the display for FRAME or the current frame if FRAME is not given or nil.
2325 If the value is 0 or the atom is not known, return the empty string. */)
2326 (Lisp_Object value, Lisp_Object frame)
2328 struct frame *f = check_x_frame (frame);
2329 char *name = 0;
2330 char empty[] = "";
2331 Lisp_Object ret = Qnil;
2332 Display *dpy = FRAME_X_DISPLAY (f);
2333 Atom atom;
2334 int had_errors;
2336 if (INTEGERP (value))
2337 atom = (Atom) XUINT (value);
2338 else if (FLOATP (value))
2339 atom = (Atom) XFLOAT_DATA (value);
2340 else if (CONSP (value))
2341 atom = (Atom) cons_to_long (value);
2342 else
2343 error ("Wrong type, value must be number or cons");
2345 BLOCK_INPUT;
2346 x_catch_errors (dpy);
2347 name = atom ? XGetAtomName (dpy, atom) : empty;
2348 had_errors = x_had_errors_p (dpy);
2349 x_uncatch_errors ();
2351 if (!had_errors)
2352 ret = make_string (name, strlen (name));
2354 if (atom && name) XFree (name);
2355 if (NILP (ret)) ret = empty_unibyte_string;
2357 UNBLOCK_INPUT;
2359 return ret;
2362 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2363 Sx_register_dnd_atom, 1, 2, 0,
2364 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2365 ATOM can be a symbol or a string. The ATOM is interned on the display that
2366 FRAME is on. If FRAME is nil, the selected frame is used. */)
2367 (Lisp_Object atom, Lisp_Object frame)
2369 Atom x_atom;
2370 struct frame *f = check_x_frame (frame);
2371 size_t i;
2372 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2375 if (SYMBOLP (atom))
2376 x_atom = symbol_to_x_atom (dpyinfo, atom);
2377 else if (STRINGP (atom))
2379 BLOCK_INPUT;
2380 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2381 UNBLOCK_INPUT;
2383 else
2384 error ("ATOM must be a symbol or a string");
2386 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2387 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2388 return Qnil;
2390 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2392 dpyinfo->x_dnd_atoms_size *= 2;
2393 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2394 sizeof (*dpyinfo->x_dnd_atoms)
2395 * dpyinfo->x_dnd_atoms_size);
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. */
2405 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2407 Lisp_Object vec;
2408 Lisp_Object frame;
2409 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2410 unsigned long size = 160/event->format;
2411 int x, y;
2412 unsigned char *data = (unsigned char *) event->data.b;
2413 int idata[5];
2414 size_t i;
2416 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2417 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2419 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2421 XSETFRAME (frame, f);
2423 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2424 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2425 function expects them to be of size int (i.e. 32). So to be able to
2426 use that function, put the data in the form it expects if format is 32. */
2428 if (32 < BITS_PER_LONG && event->format == 32)
2430 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2431 idata[i] = (int) event->data.l[i];
2432 data = (unsigned char *) idata;
2435 vec = Fmake_vector (make_number (4), Qnil);
2436 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2437 event->message_type)));
2438 ASET (vec, 1, frame);
2439 ASET (vec, 2, make_number (event->format));
2440 ASET (vec, 3, x_property_data_to_lisp (f,
2441 data,
2442 event->message_type,
2443 event->format,
2444 size));
2446 mouse_position_for_drop (f, &x, &y);
2447 bufp->kind = DRAG_N_DROP_EVENT;
2448 bufp->frame_or_window = frame;
2449 bufp->timestamp = CurrentTime;
2450 bufp->x = make_number (x);
2451 bufp->y = make_number (y);
2452 bufp->arg = vec;
2453 bufp->modifiers = 0;
2455 return 1;
2458 DEFUN ("x-send-client-message", Fx_send_client_event,
2459 Sx_send_client_message, 6, 6, 0,
2460 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2462 For DISPLAY, specify either a frame or a display name (a string).
2463 If DISPLAY is nil, that stands for the selected frame's display.
2464 DEST may be a number, in which case it is a Window id. The value 0 may
2465 be used to send to the root window of the DISPLAY.
2466 If DEST is a cons, it is converted to a 32 bit number
2467 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2468 number is then used as a window id.
2469 If DEST is a frame the event is sent to the outer window of that frame.
2470 A value of nil means the currently selected frame.
2471 If DEST is the string "PointerWindow" the event is sent to the window that
2472 contains the pointer. If DEST is the string "InputFocus" the event is
2473 sent to the window that has the input focus.
2474 FROM is the frame sending the event. Use nil for currently selected frame.
2475 MESSAGE-TYPE is the name of an Atom as a string.
2476 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2477 bits. VALUES is a list of numbers, cons and/or strings containing the values
2478 to send. If a value is a string, it is converted to an Atom and the value of
2479 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2480 with the high 16 bits from the car and the lower 16 bit from the cdr.
2481 If more values than fits into the event is given, the excessive values
2482 are ignored. */)
2483 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2485 struct x_display_info *dpyinfo = check_x_display_info (display);
2487 CHECK_STRING (message_type);
2488 x_send_client_event(display, dest, from,
2489 XInternAtom (dpyinfo->display,
2490 SSDATA (message_type),
2491 False),
2492 format, values);
2494 return Qnil;
2497 void
2498 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2500 struct x_display_info *dpyinfo = check_x_display_info (display);
2501 Window wdest;
2502 XEvent event;
2503 struct frame *f = check_x_frame (from);
2504 int to_root;
2506 CHECK_NUMBER (format);
2507 CHECK_CONS (values);
2509 if (x_check_property_data (values) == -1)
2510 error ("Bad data in VALUES, must be number, cons or string");
2512 event.xclient.type = ClientMessage;
2513 event.xclient.format = XFASTINT (format);
2515 if (event.xclient.format != 8 && event.xclient.format != 16
2516 && event.xclient.format != 32)
2517 error ("FORMAT must be one of 8, 16 or 32");
2519 if (FRAMEP (dest) || NILP (dest))
2521 struct frame *fdest = check_x_frame (dest);
2522 wdest = FRAME_OUTER_WINDOW (fdest);
2524 else if (STRINGP (dest))
2526 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2527 wdest = PointerWindow;
2528 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2529 wdest = InputFocus;
2530 else
2531 error ("DEST as a string must be one of PointerWindow or InputFocus");
2533 else if (INTEGERP (dest))
2534 wdest = (Window) XFASTINT (dest);
2535 else if (FLOATP (dest))
2536 wdest = (Window) XFLOAT_DATA (dest);
2537 else if (CONSP (dest))
2539 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2540 error ("Both car and cdr for DEST must be numbers");
2541 else
2542 wdest = (Window) cons_to_long (dest);
2544 else
2545 error ("DEST must be a frame, nil, string, number or cons");
2547 if (wdest == 0) wdest = dpyinfo->root_window;
2548 to_root = wdest == dpyinfo->root_window;
2550 BLOCK_INPUT;
2552 event.xclient.message_type = message_type;
2553 event.xclient.display = dpyinfo->display;
2555 /* Some clients (metacity for example) expects sending window to be here
2556 when sending to the root window. */
2557 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2560 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2561 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2562 event.xclient.format);
2564 /* If event mask is 0 the event is sent to the client that created
2565 the destination window. But if we are sending to the root window,
2566 there is no such client. Then we set the event mask to 0xffff. The
2567 event then goes to clients selecting for events on the root window. */
2568 x_catch_errors (dpyinfo->display);
2570 int propagate = to_root ? False : True;
2571 unsigned mask = to_root ? 0xffff : 0;
2572 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2573 XFlush (dpyinfo->display);
2575 x_uncatch_errors ();
2576 UNBLOCK_INPUT;
2580 void
2581 syms_of_xselect (void)
2583 defsubr (&Sx_get_selection_internal);
2584 defsubr (&Sx_own_selection_internal);
2585 defsubr (&Sx_disown_selection_internal);
2586 defsubr (&Sx_selection_owner_p);
2587 defsubr (&Sx_selection_exists_p);
2588 defsubr (&Sx_clipboard_manager_save);
2590 defsubr (&Sx_get_atom_name);
2591 defsubr (&Sx_send_client_message);
2592 defsubr (&Sx_register_dnd_atom);
2594 reading_selection_reply = Fcons (Qnil, Qnil);
2595 staticpro (&reading_selection_reply);
2596 reading_selection_window = 0;
2597 reading_which_selection = 0;
2599 property_change_wait_list = 0;
2600 prop_location_identifier = 0;
2601 property_change_reply = Fcons (Qnil, Qnil);
2602 staticpro (&property_change_reply);
2604 converted_selections = NULL;
2605 conversion_fail_tag = None;
2607 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2608 doc: /* An alist associating X Windows selection-types with functions.
2609 These functions are called to convert the selection, with three args:
2610 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2611 a desired type to which the selection should be converted;
2612 and the local selection value (whatever was given to `x-own-selection').
2614 The function should return the value to send to the X server
2615 \(typically a string). A return value of nil
2616 means that the conversion could not be done.
2617 A return value which is the symbol `NULL'
2618 means that a side-effect was executed,
2619 and there is no meaningful selection value. */);
2620 Vselection_converter_alist = Qnil;
2622 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2623 doc: /* A list of functions to be called when Emacs loses an X selection.
2624 \(This happens when some other X client makes its own selection
2625 or when a Lisp program explicitly clears the selection.)
2626 The functions are called with one argument, the selection type
2627 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2628 Vx_lost_selection_functions = Qnil;
2630 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2631 doc: /* A list of functions to be called when Emacs answers a selection request.
2632 The functions are called with three arguments:
2633 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2634 - the selection-type which Emacs was asked to convert the
2635 selection into before sending (for example, `STRING' or `LENGTH');
2636 - a flag indicating success or failure for responding to the request.
2637 We might have failed (and declined the request) for any number of reasons,
2638 including being asked for a selection that we no longer own, or being asked
2639 to convert into a type that we don't know about or that is inappropriate.
2640 This hook doesn't let you change the behavior of Emacs's selection replies,
2641 it merely informs you that they have happened. */);
2642 Vx_sent_selection_functions = Qnil;
2644 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2645 doc: /* Number of milliseconds to wait for a selection reply.
2646 If the selection owner doesn't reply in this time, we give up.
2647 A value of 0 means wait as long as necessary. This is initialized from the
2648 \"*selectionTimeout\" resource. */);
2649 x_selection_timeout = 0;
2651 /* QPRIMARY is defined in keyboard.c. */
2652 DEFSYM (QSECONDARY, "SECONDARY");
2653 DEFSYM (QSTRING, "STRING");
2654 DEFSYM (QINTEGER, "INTEGER");
2655 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2656 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2657 DEFSYM (QTEXT, "TEXT");
2658 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2659 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2660 DEFSYM (QDELETE, "DELETE");
2661 DEFSYM (QMULTIPLE, "MULTIPLE");
2662 DEFSYM (QINCR, "INCR");
2663 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2664 DEFSYM (QTARGETS, "TARGETS");
2665 DEFSYM (QATOM, "ATOM");
2666 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2667 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2668 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2669 DEFSYM (QNULL, "NULL");
2670 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2671 DEFSYM (Qforeign_selection, "foreign-selection");