Include <unistd.h> unilaterally.
[emacs.git] / src / xselect.c
blobed064584cc5e54ca65f99c42db38537747217bd3
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* Rewritten by jwz */
23 #include <config.h>
24 #include <stdio.h> /* termhooks.h needs this */
25 #include <setjmp.h>
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
31 #include <unistd.h>
33 #include "lisp.h"
34 #include "xterm.h" /* for all of the X includes */
35 #include "dispextern.h" /* frame.h seems to want this */
36 #include "frame.h" /* Need this to get the X window of selected_frame */
37 #include "blockinput.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;
47 static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
48 static Atom symbol_to_x_atom (struct x_display_info *, Display *,
49 Lisp_Object);
50 static void x_own_selection (Lisp_Object, Lisp_Object);
51 static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int);
52 static void x_decline_selection_request (struct input_event *);
53 static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
54 static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
55 static Lisp_Object some_frame_on_display (struct x_display_info *);
56 static Lisp_Object x_catch_errors_unwind (Lisp_Object);
57 static void x_reply_selection_request (struct input_event *, int,
58 unsigned char *, int, Atom);
59 static int waiting_for_other_props_on_window (Display *, Window);
60 static struct prop_location *expect_property_change (Display *, Window,
61 Atom, int);
62 static void unexpect_property_change (struct prop_location *);
63 static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
64 static void wait_for_property_change (struct prop_location *);
65 static Lisp_Object x_get_foreign_selection (Lisp_Object,
66 Lisp_Object,
67 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 #define TRACE3(fmt, a0, a1) (void) 0
102 #endif
105 Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
106 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
107 QATOM_PAIR;
109 Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
110 Lisp_Object QUTF8_STRING; /* This is a type of selection. */
112 Lisp_Object Qcompound_text_with_extensions;
114 static Lisp_Object Vx_lost_selection_functions;
115 static Lisp_Object Vx_sent_selection_functions;
116 static Lisp_Object Qforeign_selection;
118 /* If this is a smaller number than the max-request-size of the display,
119 emacs will use INCR selection transfer when the selection is larger
120 than this. The max-request-size is usually around 64k, so if you want
121 emacs to use incremental selection transfers when the selection is
122 smaller than that, set this. I added this mostly for debugging the
123 incremental transfer stuff, but it might improve server performance. */
124 #define MAX_SELECTION_QUANTUM 0xFFFFFF
126 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
128 /* The timestamp of the last input event Emacs received from the X server. */
129 /* Defined in keyboard.c. */
130 extern unsigned long last_event_timestamp;
132 /* This is an association list whose elements are of the form
133 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
134 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
135 SELECTION-VALUE is the value that emacs owns for that selection.
136 It may be any kind of Lisp object.
137 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
138 as a cons of two 16-bit numbers (making a 32 bit time.)
139 FRAME is the frame for which we made the selection.
140 If there is an entry in this alist, then it can be assumed that Emacs owns
141 that selection.
142 The only (eq) parts of this list that are visible from Lisp are the
143 selection-values. */
144 static Lisp_Object Vselection_alist;
146 /* This is an alist whose CARs are selection-types (whose names are the same
147 as the names of X Atoms) and whose CDRs are the names of Lisp functions to
148 call to convert the given Emacs selection value to a string representing
149 the given selection type. This is for Lisp-level extension of the emacs
150 selection handling. */
151 static Lisp_Object Vselection_converter_alist;
153 /* If the selection owner takes too long to reply to a selection request,
154 we give up on it. This is in milliseconds (0 = no timeout.) */
155 static EMACS_INT x_selection_timeout;
159 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
160 handling. */
162 struct selection_event_queue
164 struct input_event event;
165 struct selection_event_queue *next;
168 static struct selection_event_queue *selection_queue;
170 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
172 static int x_queue_selection_requests;
174 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
176 static void
177 x_queue_event (struct input_event *event)
179 struct selection_event_queue *queue_tmp;
181 /* Don't queue repeated requests.
182 This only happens for large requests which uses the incremental protocol. */
183 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
185 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
187 TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp);
188 x_decline_selection_request (event);
189 return;
193 queue_tmp
194 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
196 if (queue_tmp != NULL)
198 TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
199 queue_tmp->event = *event;
200 queue_tmp->next = selection_queue;
201 selection_queue = queue_tmp;
205 /* Start queuing SELECTION_REQUEST_EVENT events. */
207 static void
208 x_start_queuing_selection_requests (void)
210 if (x_queue_selection_requests)
211 abort ();
213 x_queue_selection_requests++;
214 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
217 /* Stop queuing SELECTION_REQUEST_EVENT events. */
219 static void
220 x_stop_queuing_selection_requests (void)
222 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
223 --x_queue_selection_requests;
225 /* Take all the queued events and put them back
226 so that they get processed afresh. */
228 while (selection_queue != NULL)
230 struct selection_event_queue *queue_tmp = selection_queue;
231 TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
232 kbd_buffer_unget_event (&queue_tmp->event);
233 selection_queue = queue_tmp->next;
234 xfree ((char *)queue_tmp);
239 /* This converts a Lisp symbol to a server Atom, avoiding a server
240 roundtrip whenever possible. */
242 static Atom
243 symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym)
245 Atom val;
246 if (NILP (sym)) return 0;
247 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
248 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
249 if (EQ (sym, QSTRING)) return XA_STRING;
250 if (EQ (sym, QINTEGER)) return XA_INTEGER;
251 if (EQ (sym, QATOM)) return XA_ATOM;
252 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
253 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
254 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
255 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
256 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
257 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
258 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
259 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
260 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
261 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
262 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
263 if (!SYMBOLP (sym)) abort ();
265 TRACE1 (" XInternAtom %s", (char *) SDATA (SYMBOL_NAME (sym)));
266 BLOCK_INPUT;
267 val = XInternAtom (display, (char *) SDATA (SYMBOL_NAME (sym)), False);
268 UNBLOCK_INPUT;
269 return val;
273 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
274 and calls to intern whenever possible. */
276 static Lisp_Object
277 x_atom_to_symbol (Display *dpy, Atom atom)
279 struct x_display_info *dpyinfo;
280 char *str;
281 Lisp_Object val;
283 if (! atom)
284 return Qnil;
286 switch (atom)
288 case XA_PRIMARY:
289 return QPRIMARY;
290 case XA_SECONDARY:
291 return QSECONDARY;
292 case XA_STRING:
293 return QSTRING;
294 case XA_INTEGER:
295 return QINTEGER;
296 case XA_ATOM:
297 return QATOM;
300 dpyinfo = x_display_info_for_display (dpy);
301 if (atom == dpyinfo->Xatom_CLIPBOARD)
302 return QCLIPBOARD;
303 if (atom == dpyinfo->Xatom_TIMESTAMP)
304 return QTIMESTAMP;
305 if (atom == dpyinfo->Xatom_TEXT)
306 return QTEXT;
307 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
308 return QCOMPOUND_TEXT;
309 if (atom == dpyinfo->Xatom_UTF8_STRING)
310 return QUTF8_STRING;
311 if (atom == dpyinfo->Xatom_DELETE)
312 return QDELETE;
313 if (atom == dpyinfo->Xatom_MULTIPLE)
314 return QMULTIPLE;
315 if (atom == dpyinfo->Xatom_INCR)
316 return QINCR;
317 if (atom == dpyinfo->Xatom_EMACS_TMP)
318 return QEMACS_TMP;
319 if (atom == dpyinfo->Xatom_TARGETS)
320 return QTARGETS;
321 if (atom == dpyinfo->Xatom_NULL)
322 return QNULL;
324 BLOCK_INPUT;
325 str = XGetAtomName (dpy, atom);
326 UNBLOCK_INPUT;
327 TRACE1 ("XGetAtomName --> %s", str);
328 if (! str) return Qnil;
329 val = intern (str);
330 BLOCK_INPUT;
331 /* This was allocated by Xlib, so use XFree. */
332 XFree (str);
333 UNBLOCK_INPUT;
334 return val;
337 /* Do protocol to assert ourself as a selection owner.
338 Update the Vselection_alist so that we can reply to later requests for
339 our selection. */
341 static void
342 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
344 struct frame *sf = SELECTED_FRAME ();
345 Window selecting_window;
346 Display *display;
347 Time time = last_event_timestamp;
348 Atom selection_atom;
349 struct x_display_info *dpyinfo;
351 if (! FRAME_X_P (sf))
352 return;
354 selecting_window = FRAME_X_WINDOW (sf);
355 display = FRAME_X_DISPLAY (sf);
356 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
358 CHECK_SYMBOL (selection_name);
359 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
361 BLOCK_INPUT;
362 x_catch_errors (display);
363 XSetSelectionOwner (display, selection_atom, selecting_window, time);
364 x_check_errors (display, "Can't set selection: %s");
365 x_uncatch_errors ();
366 UNBLOCK_INPUT;
368 /* Now update the local cache */
370 Lisp_Object selection_time;
371 Lisp_Object selection_data;
372 Lisp_Object prev_value;
374 selection_time = long_to_cons ((unsigned long) time);
375 selection_data = list4 (selection_name, selection_value,
376 selection_time, selected_frame);
377 prev_value = assq_no_quit (selection_name, Vselection_alist);
379 Vselection_alist = Fcons (selection_data, Vselection_alist);
381 /* If we already owned the selection, remove the old selection data.
382 Perhaps we should destructively modify it instead.
383 Don't use Fdelq as that may QUIT. */
384 if (!NILP (prev_value))
386 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
387 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
388 if (EQ (prev_value, Fcar (XCDR (rest))))
390 XSETCDR (rest, Fcdr (XCDR (rest)));
391 break;
397 /* Given a selection-name and desired type, look up our local copy of
398 the selection value and convert it to the type.
399 The value is nil or a string.
400 This function is used both for remote requests (LOCAL_REQUEST is zero)
401 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
403 This calls random Lisp code, and may signal or gc. */
405 static Lisp_Object
406 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request)
408 Lisp_Object local_value;
409 Lisp_Object handler_fn, value, type, check;
410 int count;
412 local_value = assq_no_quit (selection_symbol, Vselection_alist);
414 if (NILP (local_value)) return Qnil;
416 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
417 if (EQ (target_type, QTIMESTAMP))
419 handler_fn = Qnil;
420 value = XCAR (XCDR (XCDR (local_value)));
422 #if 0
423 else if (EQ (target_type, QDELETE))
425 handler_fn = Qnil;
426 Fx_disown_selection_internal
427 (selection_symbol,
428 XCAR (XCDR (XCDR (local_value))));
429 value = QNULL;
431 #endif
433 #if 0 /* #### MULTIPLE doesn't work yet */
434 else if (CONSP (target_type)
435 && XCAR (target_type) == QMULTIPLE)
437 Lisp_Object pairs;
438 int size;
439 int i;
440 pairs = XCDR (target_type);
441 size = XVECTOR (pairs)->size;
442 /* If the target is MULTIPLE, then target_type looks like
443 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
444 We modify the second element of each pair in the vector and
445 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
447 for (i = 0; i < size; i++)
449 Lisp_Object pair;
450 pair = XVECTOR (pairs)->contents [i];
451 XVECTOR (pair)->contents [1]
452 = x_get_local_selection (XVECTOR (pair)->contents [0],
453 XVECTOR (pair)->contents [1],
454 local_request);
456 return pairs;
458 #endif
459 else
461 /* Don't allow a quit within the converter.
462 When the user types C-g, he would be surprised
463 if by luck it came during a converter. */
464 count = SPECPDL_INDEX ();
465 specbind (Qinhibit_quit, Qt);
467 CHECK_SYMBOL (target_type);
468 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
469 /* gcpro is not needed here since nothing but HANDLER_FN
470 is live, and that ought to be a symbol. */
472 if (!NILP (handler_fn))
473 value = call3 (handler_fn,
474 selection_symbol, (local_request ? Qnil : target_type),
475 XCAR (XCDR (local_value)));
476 else
477 value = Qnil;
478 unbind_to (count, Qnil);
481 /* Make sure this value is of a type that we could transmit
482 to another X client. */
484 check = value;
485 if (CONSP (value)
486 && SYMBOLP (XCAR (value)))
487 type = XCAR (value),
488 check = XCDR (value);
490 if (STRINGP (check)
491 || VECTORP (check)
492 || SYMBOLP (check)
493 || INTEGERP (check)
494 || NILP (value))
495 return value;
496 /* Check for a value that cons_to_long could handle. */
497 else if (CONSP (check)
498 && INTEGERP (XCAR (check))
499 && (INTEGERP (XCDR (check))
501 (CONSP (XCDR (check))
502 && INTEGERP (XCAR (XCDR (check)))
503 && NILP (XCDR (XCDR (check))))))
504 return value;
506 signal_error ("Invalid data returned by selection-conversion function",
507 list2 (handler_fn, value));
510 /* Subroutines of x_reply_selection_request. */
512 /* Send a SelectionNotify event to the requestor with property=None,
513 meaning we were unable to do what they wanted. */
515 static void
516 x_decline_selection_request (struct input_event *event)
518 XSelectionEvent reply;
520 reply.type = SelectionNotify;
521 reply.display = SELECTION_EVENT_DISPLAY (event);
522 reply.requestor = SELECTION_EVENT_REQUESTOR (event);
523 reply.selection = SELECTION_EVENT_SELECTION (event);
524 reply.time = SELECTION_EVENT_TIME (event);
525 reply.target = SELECTION_EVENT_TARGET (event);
526 reply.property = None;
528 /* The reason for the error may be that the receiver has
529 died in the meantime. Handle that case. */
530 BLOCK_INPUT;
531 x_catch_errors (reply.display);
532 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
533 XFlush (reply.display);
534 x_uncatch_errors ();
535 UNBLOCK_INPUT;
538 /* This is the selection request currently being processed.
539 It is set to zero when the request is fully processed. */
540 static struct input_event *x_selection_current_request;
542 /* Display info in x_selection_request. */
544 static struct x_display_info *selection_request_dpyinfo;
546 /* Used as an unwind-protect clause so that, if a selection-converter signals
547 an error, we tell the requester that we were unable to do what they wanted
548 before we throw to top-level or go into the debugger or whatever. */
550 static Lisp_Object
551 x_selection_request_lisp_error (Lisp_Object ignore)
553 if (x_selection_current_request != 0
554 && selection_request_dpyinfo->display)
555 x_decline_selection_request (x_selection_current_request);
556 return Qnil;
559 static Lisp_Object
560 x_catch_errors_unwind (Lisp_Object dummy)
562 BLOCK_INPUT;
563 x_uncatch_errors ();
564 UNBLOCK_INPUT;
565 return Qnil;
569 /* This stuff is so that INCR selections are reentrant (that is, so we can
570 be servicing multiple INCR selection requests simultaneously.) I haven't
571 actually tested that yet. */
573 /* Keep a list of the property changes that are awaited. */
575 struct prop_location
577 int identifier;
578 Display *display;
579 Window window;
580 Atom property;
581 int desired_state;
582 int arrived;
583 struct prop_location *next;
586 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
587 static void wait_for_property_change (struct prop_location *location);
588 static void unexpect_property_change (struct prop_location *location);
589 static int waiting_for_other_props_on_window (Display *display, Window window);
591 static int prop_location_identifier;
593 static Lisp_Object property_change_reply;
595 static struct prop_location *property_change_reply_object;
597 static struct prop_location *property_change_wait_list;
599 static Lisp_Object
600 queue_selection_requests_unwind (Lisp_Object tem)
602 x_stop_queuing_selection_requests ();
603 return Qnil;
606 /* Return some frame whose display info is DPYINFO.
607 Return nil if there is none. */
609 static Lisp_Object
610 some_frame_on_display (struct x_display_info *dpyinfo)
612 Lisp_Object list, frame;
614 FOR_EACH_FRAME (list, frame)
616 if (FRAME_X_P (XFRAME (frame))
617 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
618 return frame;
621 return Qnil;
624 /* Send the reply to a selection request event EVENT.
625 TYPE is the type of selection data requested.
626 DATA and SIZE describe the data to send, already converted.
627 FORMAT is the unit-size (in bits) of the data to be transmitted. */
629 #ifdef TRACE_SELECTION
630 static int x_reply_selection_request_cnt;
631 #endif /* TRACE_SELECTION */
633 static void
634 x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type)
636 XSelectionEvent reply;
637 Display *display = SELECTION_EVENT_DISPLAY (event);
638 Window window = SELECTION_EVENT_REQUESTOR (event);
639 int bytes_remaining;
640 int format_bytes = format/8;
641 int max_bytes = SELECTION_QUANTUM (display);
642 struct x_display_info *dpyinfo = x_display_info_for_display (display);
643 int count = SPECPDL_INDEX ();
645 if (max_bytes > MAX_SELECTION_QUANTUM)
646 max_bytes = MAX_SELECTION_QUANTUM;
648 reply.type = SelectionNotify;
649 reply.display = display;
650 reply.requestor = window;
651 reply.selection = SELECTION_EVENT_SELECTION (event);
652 reply.time = SELECTION_EVENT_TIME (event);
653 reply.target = SELECTION_EVENT_TARGET (event);
654 reply.property = SELECTION_EVENT_PROPERTY (event);
655 if (reply.property == None)
656 reply.property = reply.target;
658 BLOCK_INPUT;
659 /* The protected block contains wait_for_property_change, which can
660 run random lisp code (process handlers) or signal. Therefore, we
661 put the x_uncatch_errors call in an unwind. */
662 record_unwind_protect (x_catch_errors_unwind, Qnil);
663 x_catch_errors (display);
665 #ifdef TRACE_SELECTION
667 char *sel = XGetAtomName (display, reply.selection);
668 char *tgt = XGetAtomName (display, reply.target);
669 TRACE3 ("%s, target %s (%d)", sel, tgt, ++x_reply_selection_request_cnt);
670 if (sel) XFree (sel);
671 if (tgt) XFree (tgt);
673 #endif /* TRACE_SELECTION */
675 /* Store the data on the requested property.
676 If the selection is large, only store the first N bytes of it.
678 bytes_remaining = size * format_bytes;
679 if (bytes_remaining <= max_bytes)
681 /* Send all the data at once, with minimal handshaking. */
682 TRACE1 ("Sending all %d bytes", bytes_remaining);
683 XChangeProperty (display, window, reply.property, type, format,
684 PropModeReplace, data, size);
685 /* At this point, the selection was successfully stored; ack it. */
686 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
688 else
690 /* Send an INCR selection. */
691 struct prop_location *wait_object;
692 int had_errors;
693 Lisp_Object frame;
695 frame = some_frame_on_display (dpyinfo);
697 /* If the display no longer has frames, we can't expect
698 to get many more selection requests from it, so don't
699 bother trying to queue them. */
700 if (!NILP (frame))
702 x_start_queuing_selection_requests ();
704 record_unwind_protect (queue_selection_requests_unwind,
705 Qnil);
708 if (x_window_to_frame (dpyinfo, window)) /* #### debug */
709 error ("Attempt to transfer an INCR to ourself!");
711 TRACE2 ("Start sending %d bytes incrementally (%s)",
712 bytes_remaining, XGetAtomName (display, reply.property));
713 wait_object = expect_property_change (display, window, reply.property,
714 PropertyDelete);
716 TRACE1 ("Set %s to number of bytes to send",
717 XGetAtomName (display, reply.property));
719 /* XChangeProperty expects an array of long even if long is more than
720 32 bits. */
721 long value[1];
723 value[0] = bytes_remaining;
724 XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
725 32, PropModeReplace,
726 (unsigned char *) value, 1);
729 XSelectInput (display, window, PropertyChangeMask);
731 /* Tell 'em the INCR data is there... */
732 TRACE0 ("Send SelectionNotify event");
733 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
734 XFlush (display);
736 had_errors = x_had_errors_p (display);
737 UNBLOCK_INPUT;
739 /* First, wait for the requester to ack by deleting the property.
740 This can run random lisp code (process handlers) or signal. */
741 if (! had_errors)
743 TRACE1 ("Waiting for ACK (deletion of %s)",
744 XGetAtomName (display, reply.property));
745 wait_for_property_change (wait_object);
747 else
748 unexpect_property_change (wait_object);
750 TRACE0 ("Got ACK");
751 while (bytes_remaining)
753 int i = ((bytes_remaining < max_bytes)
754 ? bytes_remaining
755 : max_bytes) / format_bytes;
757 BLOCK_INPUT;
759 wait_object
760 = expect_property_change (display, window, reply.property,
761 PropertyDelete);
763 TRACE1 ("Sending increment of %d elements", i);
764 TRACE1 ("Set %s to increment data",
765 XGetAtomName (display, reply.property));
767 /* Append the next chunk of data to the property. */
768 XChangeProperty (display, window, reply.property, type, format,
769 PropModeAppend, data, i);
770 bytes_remaining -= i * format_bytes;
771 if (format == 32)
772 data += i * sizeof (long);
773 else
774 data += i * format_bytes;
775 XFlush (display);
776 had_errors = x_had_errors_p (display);
777 UNBLOCK_INPUT;
779 if (had_errors)
780 break;
782 /* Now wait for the requester to ack this chunk by deleting the
783 property. This can run random lisp code or signal. */
784 TRACE1 ("Waiting for increment ACK (deletion of %s)",
785 XGetAtomName (display, reply.property));
786 wait_for_property_change (wait_object);
789 /* Now write a zero-length chunk to the property to tell the
790 requester that we're done. */
791 BLOCK_INPUT;
792 if (! waiting_for_other_props_on_window (display, window))
793 XSelectInput (display, window, 0L);
795 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
796 XGetAtomName (display, reply.property));
797 XChangeProperty (display, window, reply.property, type, format,
798 PropModeReplace, data, 0);
799 TRACE0 ("Done sending incrementally");
802 /* rms, 2003-01-03: I think I have fixed this bug. */
803 /* The window we're communicating with may have been deleted
804 in the meantime (that's a real situation from a bug report).
805 In this case, there may be events in the event queue still
806 refering to the deleted window, and we'll get a BadWindow error
807 in XTread_socket when processing the events. I don't have
808 an idea how to fix that. gerd, 2001-01-98. */
809 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
810 delivered before uncatch errors. */
811 XSync (display, False);
812 UNBLOCK_INPUT;
814 /* GTK queues events in addition to the queue in Xlib. So we
815 UNBLOCK to enter the event loop and get possible errors delivered,
816 and then BLOCK again because x_uncatch_errors requires it. */
817 BLOCK_INPUT;
818 /* This calls x_uncatch_errors. */
819 unbind_to (count, Qnil);
820 UNBLOCK_INPUT;
823 /* Handle a SelectionRequest event EVENT.
824 This is called from keyboard.c when such an event is found in the queue. */
826 static void
827 x_handle_selection_request (struct input_event *event)
829 struct gcpro gcpro1, gcpro2, gcpro3;
830 Lisp_Object local_selection_data;
831 Lisp_Object selection_symbol;
832 Lisp_Object target_symbol;
833 Lisp_Object converted_selection;
834 Time local_selection_time;
835 Lisp_Object successful_p;
836 int count;
837 struct x_display_info *dpyinfo
838 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
840 TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
841 (unsigned long) SELECTION_EVENT_REQUESTOR (event),
842 (unsigned long) SELECTION_EVENT_TIME (event));
844 local_selection_data = Qnil;
845 target_symbol = Qnil;
846 converted_selection = Qnil;
847 successful_p = Qnil;
849 GCPRO3 (local_selection_data, converted_selection, target_symbol);
851 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
852 SELECTION_EVENT_SELECTION (event));
854 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
856 if (NILP (local_selection_data))
858 /* Someone asked for the selection, but we don't have it any more.
860 x_decline_selection_request (event);
861 goto DONE;
864 local_selection_time = (Time)
865 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
867 if (SELECTION_EVENT_TIME (event) != CurrentTime
868 && local_selection_time > SELECTION_EVENT_TIME (event))
870 /* Someone asked for the selection, and we have one, but not the one
871 they're looking for.
873 x_decline_selection_request (event);
874 goto DONE;
877 x_selection_current_request = event;
878 count = SPECPDL_INDEX ();
879 selection_request_dpyinfo = dpyinfo;
880 record_unwind_protect (x_selection_request_lisp_error, Qnil);
882 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
883 SELECTION_EVENT_TARGET (event));
885 #if 0 /* #### MULTIPLE doesn't work yet */
886 if (EQ (target_symbol, QMULTIPLE))
887 target_symbol = fetch_multiple_target (event);
888 #endif
890 /* Convert lisp objects back into binary data */
892 converted_selection
893 = x_get_local_selection (selection_symbol, target_symbol, 0);
895 if (! NILP (converted_selection))
897 unsigned char *data;
898 unsigned int size;
899 int format;
900 Atom type;
901 int nofree;
903 if (CONSP (converted_selection) && NILP (XCDR (converted_selection)))
905 x_decline_selection_request (event);
906 goto DONE2;
909 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
910 converted_selection,
911 &data, &type, &size, &format, &nofree);
913 x_reply_selection_request (event, format, data, size, type);
914 successful_p = Qt;
916 /* Indicate we have successfully processed this event. */
917 x_selection_current_request = 0;
919 /* Use xfree, not XFree, because lisp_data_to_selection_data
920 calls xmalloc itself. */
921 if (!nofree)
922 xfree (data);
925 DONE2:
926 unbind_to (count, Qnil);
928 DONE:
930 /* Let random lisp code notice that the selection has been asked for. */
932 Lisp_Object rest;
933 rest = Vx_sent_selection_functions;
934 if (!EQ (rest, Qunbound))
935 for (; CONSP (rest); rest = Fcdr (rest))
936 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
939 UNGCPRO;
942 /* Handle a SelectionClear event EVENT, which indicates that some
943 client cleared out our previously asserted selection.
944 This is called from keyboard.c when such an event is found in the queue. */
946 static void
947 x_handle_selection_clear (struct input_event *event)
949 Display *display = SELECTION_EVENT_DISPLAY (event);
950 Atom selection = SELECTION_EVENT_SELECTION (event);
951 Time changed_owner_time = SELECTION_EVENT_TIME (event);
953 Lisp_Object selection_symbol, local_selection_data;
954 Time local_selection_time;
955 struct x_display_info *dpyinfo = x_display_info_for_display (display);
956 struct x_display_info *t_dpyinfo;
958 TRACE0 ("x_handle_selection_clear");
960 /* If the new selection owner is also Emacs,
961 don't clear the new selection. */
962 BLOCK_INPUT;
963 /* Check each display on the same terminal,
964 to see if this Emacs job now owns the selection
965 through that display. */
966 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
967 if (t_dpyinfo->terminal->kboard == dpyinfo->terminal->kboard)
969 Window owner_window
970 = XGetSelectionOwner (t_dpyinfo->display, selection);
971 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
973 UNBLOCK_INPUT;
974 return;
977 UNBLOCK_INPUT;
979 selection_symbol = x_atom_to_symbol (display, selection);
981 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
983 /* Well, we already believe that we don't own it, so that's just fine. */
984 if (NILP (local_selection_data)) return;
986 local_selection_time = (Time)
987 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
989 /* This SelectionClear is for a selection that we no longer own, so we can
990 disregard it. (That is, we have reasserted the selection since this
991 request was generated.) */
993 if (changed_owner_time != CurrentTime
994 && local_selection_time > changed_owner_time)
995 return;
997 /* Otherwise, we're really honest and truly being told to drop it.
998 Don't use Fdelq as that may QUIT;. */
1000 if (EQ (local_selection_data, Fcar (Vselection_alist)))
1001 Vselection_alist = Fcdr (Vselection_alist);
1002 else
1004 Lisp_Object rest;
1005 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
1006 if (EQ (local_selection_data, Fcar (XCDR (rest))))
1008 XSETCDR (rest, Fcdr (XCDR (rest)));
1009 break;
1013 /* Let random lisp code notice that the selection has been stolen. */
1016 Lisp_Object rest;
1017 rest = Vx_lost_selection_functions;
1018 if (!EQ (rest, Qunbound))
1020 for (; CONSP (rest); rest = Fcdr (rest))
1021 call1 (Fcar (rest), selection_symbol);
1022 prepare_menu_bars ();
1023 redisplay_preserve_echo_area (20);
1028 void
1029 x_handle_selection_event (struct input_event *event)
1031 TRACE0 ("x_handle_selection_event");
1033 if (event->kind == SELECTION_REQUEST_EVENT)
1035 if (x_queue_selection_requests)
1036 x_queue_event (event);
1037 else
1038 x_handle_selection_request (event);
1040 else
1041 x_handle_selection_clear (event);
1045 /* Clear all selections that were made from frame F.
1046 We do this when about to delete a frame. */
1048 void
1049 x_clear_frame_selections (FRAME_PTR f)
1051 Lisp_Object frame;
1052 Lisp_Object rest;
1054 XSETFRAME (frame, f);
1056 /* Otherwise, we're really honest and truly being told to drop it.
1057 Don't use Fdelq as that may QUIT;. */
1059 /* Delete elements from the beginning of Vselection_alist. */
1060 while (!NILP (Vselection_alist)
1061 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
1063 /* Let random Lisp code notice that the selection has been stolen. */
1064 Lisp_Object hooks, selection_symbol;
1066 hooks = Vx_lost_selection_functions;
1067 selection_symbol = Fcar (Fcar (Vselection_alist));
1069 if (!EQ (hooks, Qunbound))
1071 for (; CONSP (hooks); hooks = Fcdr (hooks))
1072 call1 (Fcar (hooks), selection_symbol);
1073 #if 0 /* This can crash when deleting a frame
1074 from x_connection_closed. Anyway, it seems unnecessary;
1075 something else should cause a redisplay. */
1076 redisplay_preserve_echo_area (21);
1077 #endif
1080 Vselection_alist = Fcdr (Vselection_alist);
1083 /* Delete elements after the beginning of Vselection_alist. */
1084 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
1085 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
1087 /* Let random Lisp code notice that the selection has been stolen. */
1088 Lisp_Object hooks, selection_symbol;
1090 hooks = Vx_lost_selection_functions;
1091 selection_symbol = Fcar (Fcar (XCDR (rest)));
1093 if (!EQ (hooks, Qunbound))
1095 for (; CONSP (hooks); hooks = Fcdr (hooks))
1096 call1 (Fcar (hooks), selection_symbol);
1097 #if 0 /* See above */
1098 redisplay_preserve_echo_area (22);
1099 #endif
1101 XSETCDR (rest, Fcdr (XCDR (rest)));
1102 break;
1106 /* Nonzero if any properties for DISPLAY and WINDOW
1107 are on the list of what we are waiting for. */
1109 static int
1110 waiting_for_other_props_on_window (Display *display, Window window)
1112 struct prop_location *rest = property_change_wait_list;
1113 while (rest)
1114 if (rest->display == display && rest->window == window)
1115 return 1;
1116 else
1117 rest = rest->next;
1118 return 0;
1121 /* Add an entry to the list of property changes we are waiting for.
1122 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1123 The return value is a number that uniquely identifies
1124 this awaited property change. */
1126 static struct prop_location *
1127 expect_property_change (Display *display, Window window, Atom property, int state)
1129 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1130 pl->identifier = ++prop_location_identifier;
1131 pl->display = display;
1132 pl->window = window;
1133 pl->property = property;
1134 pl->desired_state = state;
1135 pl->next = property_change_wait_list;
1136 pl->arrived = 0;
1137 property_change_wait_list = pl;
1138 return pl;
1141 /* Delete an entry from the list of property changes we are waiting for.
1142 IDENTIFIER is the number that uniquely identifies the entry. */
1144 static void
1145 unexpect_property_change (struct prop_location *location)
1147 struct prop_location *prev = 0, *rest = property_change_wait_list;
1148 while (rest)
1150 if (rest == location)
1152 if (prev)
1153 prev->next = rest->next;
1154 else
1155 property_change_wait_list = rest->next;
1156 xfree (rest);
1157 return;
1159 prev = rest;
1160 rest = rest->next;
1164 /* Remove the property change expectation element for IDENTIFIER. */
1166 static Lisp_Object
1167 wait_for_property_change_unwind (Lisp_Object loc)
1169 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1171 unexpect_property_change (location);
1172 if (location == property_change_reply_object)
1173 property_change_reply_object = 0;
1174 return Qnil;
1177 /* Actually wait for a property change.
1178 IDENTIFIER should be the value that expect_property_change returned. */
1180 static void
1181 wait_for_property_change (struct prop_location *location)
1183 int secs, usecs;
1184 int count = SPECPDL_INDEX ();
1186 if (property_change_reply_object)
1187 abort ();
1189 /* Make sure to do unexpect_property_change if we quit or err. */
1190 record_unwind_protect (wait_for_property_change_unwind,
1191 make_save_value (location, 0));
1193 XSETCAR (property_change_reply, Qnil);
1194 property_change_reply_object = location;
1196 /* If the event we are waiting for arrives beyond here, it will set
1197 property_change_reply, because property_change_reply_object says so. */
1198 if (! location->arrived)
1200 secs = x_selection_timeout / 1000;
1201 usecs = (x_selection_timeout % 1000) * 1000;
1202 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1203 wait_reading_process_output (secs, usecs, 0, 0,
1204 property_change_reply, NULL, 0);
1206 if (NILP (XCAR (property_change_reply)))
1208 TRACE0 (" Timed out");
1209 error ("Timed out waiting for property-notify event");
1213 unbind_to (count, Qnil);
1216 /* Called from XTread_socket in response to a PropertyNotify event. */
1218 void
1219 x_handle_property_notify (XPropertyEvent *event)
1221 struct prop_location *prev = 0, *rest = property_change_wait_list;
1223 while (rest)
1225 if (!rest->arrived
1226 && rest->property == event->atom
1227 && rest->window == event->window
1228 && rest->display == event->display
1229 && rest->desired_state == event->state)
1231 TRACE2 ("Expected %s of property %s",
1232 (event->state == PropertyDelete ? "deletion" : "change"),
1233 XGetAtomName (event->display, event->atom));
1235 rest->arrived = 1;
1237 /* If this is the one wait_for_property_change is waiting for,
1238 tell it to wake up. */
1239 if (rest == property_change_reply_object)
1240 XSETCAR (property_change_reply, Qt);
1242 return;
1245 prev = rest;
1246 rest = rest->next;
1252 #if 0 /* #### MULTIPLE doesn't work yet */
1254 static Lisp_Object
1255 fetch_multiple_target (event)
1256 XSelectionRequestEvent *event;
1258 Display *display = event->display;
1259 Window window = event->requestor;
1260 Atom target = event->target;
1261 Atom selection_atom = event->selection;
1262 int result;
1264 return
1265 Fcons (QMULTIPLE,
1266 x_get_window_property_as_lisp_data (display, window, target,
1267 QMULTIPLE, selection_atom));
1270 static Lisp_Object
1271 copy_multiple_data (obj)
1272 Lisp_Object obj;
1274 Lisp_Object vec;
1275 int i;
1276 int size;
1277 if (CONSP (obj))
1278 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1280 CHECK_VECTOR (obj);
1281 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1282 for (i = 0; i < size; i++)
1284 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1285 CHECK_VECTOR (vec2);
1286 if (XVECTOR (vec2)->size != 2)
1287 /* ??? Confusing error message */
1288 signal_error ("Vectors must be of length 2", vec2);
1289 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1290 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1291 = XVECTOR (vec2)->contents [0];
1292 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1293 = XVECTOR (vec2)->contents [1];
1295 return vec;
1298 #endif
1301 /* Variables for communication with x_handle_selection_notify. */
1302 static Atom reading_which_selection;
1303 static Lisp_Object reading_selection_reply;
1304 static Window reading_selection_window;
1306 /* Do protocol to read selection-data from the server.
1307 Converts this to Lisp data and returns it. */
1309 static Lisp_Object
1310 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
1312 struct frame *sf = SELECTED_FRAME ();
1313 Window requestor_window;
1314 Display *display;
1315 struct x_display_info *dpyinfo;
1316 Time requestor_time = last_event_timestamp;
1317 Atom target_property;
1318 Atom selection_atom;
1319 Atom type_atom;
1320 int secs, usecs;
1321 int count = SPECPDL_INDEX ();
1322 Lisp_Object frame;
1324 if (! FRAME_X_P (sf))
1325 return Qnil;
1327 requestor_window = FRAME_X_WINDOW (sf);
1328 display = FRAME_X_DISPLAY (sf);
1329 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1330 target_property = dpyinfo->Xatom_EMACS_TMP;
1331 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1333 if (CONSP (target_type))
1334 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1335 else
1336 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1338 if (! NILP (time_stamp))
1340 if (CONSP (time_stamp))
1341 requestor_time = (Time) cons_to_long (time_stamp);
1342 else if (INTEGERP (time_stamp))
1343 requestor_time = (Time) XUINT (time_stamp);
1344 else if (FLOATP (time_stamp))
1345 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1346 else
1347 error ("TIME_STAMP must be cons or number");
1350 BLOCK_INPUT;
1352 /* The protected block contains wait_reading_process_output, which
1353 can run random lisp code (process handlers) or signal.
1354 Therefore, we put the x_uncatch_errors call in an unwind. */
1355 record_unwind_protect (x_catch_errors_unwind, Qnil);
1356 x_catch_errors (display);
1358 TRACE2 ("Get selection %s, type %s",
1359 XGetAtomName (display, type_atom),
1360 XGetAtomName (display, target_property));
1362 XConvertSelection (display, selection_atom, type_atom, target_property,
1363 requestor_window, requestor_time);
1364 XFlush (display);
1366 /* Prepare to block until the reply has been read. */
1367 reading_selection_window = requestor_window;
1368 reading_which_selection = selection_atom;
1369 XSETCAR (reading_selection_reply, Qnil);
1371 frame = some_frame_on_display (dpyinfo);
1373 /* If the display no longer has frames, we can't expect
1374 to get many more selection requests from it, so don't
1375 bother trying to queue them. */
1376 if (!NILP (frame))
1378 x_start_queuing_selection_requests ();
1380 record_unwind_protect (queue_selection_requests_unwind,
1381 Qnil);
1383 UNBLOCK_INPUT;
1385 /* This allows quits. Also, don't wait forever. */
1386 secs = x_selection_timeout / 1000;
1387 usecs = (x_selection_timeout % 1000) * 1000;
1388 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1389 wait_reading_process_output (secs, usecs, 0, 0,
1390 reading_selection_reply, NULL, 0);
1391 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1393 BLOCK_INPUT;
1394 if (x_had_errors_p (display))
1395 error ("Cannot get selection");
1396 /* This calls x_uncatch_errors. */
1397 unbind_to (count, Qnil);
1398 UNBLOCK_INPUT;
1400 if (NILP (XCAR (reading_selection_reply)))
1401 error ("Timed out waiting for reply from selection owner");
1402 if (EQ (XCAR (reading_selection_reply), Qlambda))
1403 return Qnil;
1405 /* Otherwise, the selection is waiting for us on the requested property. */
1406 return
1407 x_get_window_property_as_lisp_data (display, requestor_window,
1408 target_property, target_type,
1409 selection_atom);
1412 /* Subroutines of x_get_window_property_as_lisp_data */
1414 /* Use xfree, not XFree, to free the data obtained with this function. */
1416 static void
1417 x_get_window_property (Display *display, Window window, Atom property,
1418 unsigned char **data_ret, int *bytes_ret,
1419 Atom *actual_type_ret, int *actual_format_ret,
1420 unsigned long *actual_size_ret, int delete_p)
1422 int total_size;
1423 unsigned long bytes_remaining;
1424 int offset = 0;
1425 unsigned char *tmp_data = 0;
1426 int result;
1427 int buffer_size = SELECTION_QUANTUM (display);
1429 if (buffer_size > MAX_SELECTION_QUANTUM)
1430 buffer_size = MAX_SELECTION_QUANTUM;
1432 BLOCK_INPUT;
1434 /* First probe the thing to find out how big it is. */
1435 result = XGetWindowProperty (display, window, property,
1436 0L, 0L, False, AnyPropertyType,
1437 actual_type_ret, actual_format_ret,
1438 actual_size_ret,
1439 &bytes_remaining, &tmp_data);
1440 if (result != Success)
1442 UNBLOCK_INPUT;
1443 *data_ret = 0;
1444 *bytes_ret = 0;
1445 return;
1448 /* This was allocated by Xlib, so use XFree. */
1449 XFree ((char *) tmp_data);
1451 if (*actual_type_ret == None || *actual_format_ret == 0)
1453 UNBLOCK_INPUT;
1454 return;
1457 total_size = bytes_remaining + 1;
1458 *data_ret = (unsigned char *) xmalloc (total_size);
1460 /* Now read, until we've gotten it all. */
1461 while (bytes_remaining)
1463 #ifdef TRACE_SELECTION
1464 int last = bytes_remaining;
1465 #endif
1466 result
1467 = XGetWindowProperty (display, window, property,
1468 (long)offset/4, (long)buffer_size/4,
1469 False,
1470 AnyPropertyType,
1471 actual_type_ret, actual_format_ret,
1472 actual_size_ret, &bytes_remaining, &tmp_data);
1474 TRACE2 ("Read %ld bytes from property %s",
1475 last - bytes_remaining,
1476 XGetAtomName (display, property));
1478 /* If this doesn't return Success at this point, it means that
1479 some clod deleted the selection while we were in the midst of
1480 reading it. Deal with that, I guess.... */
1481 if (result != Success)
1482 break;
1484 /* The man page for XGetWindowProperty says:
1485 "If the returned format is 32, the returned data is represented
1486 as a long array and should be cast to that type to obtain the
1487 elements."
1488 This applies even if long is more than 32 bits, the X library
1489 converts from 32 bit elements received from the X server to long
1490 and passes the long array to us. Thus, for that case memcpy can not
1491 be used. We convert to a 32 bit type here, because so much code
1492 assume on that.
1494 The bytes and offsets passed to XGetWindowProperty refers to the
1495 property and those are indeed in 32 bit quantities if format is 32. */
1497 if (*actual_format_ret == 32 && *actual_format_ret < BITS_PER_LONG)
1499 unsigned long i;
1500 int *idata = (int *) ((*data_ret) + offset);
1501 long *ldata = (long *) tmp_data;
1503 for (i = 0; i < *actual_size_ret; ++i)
1505 idata[i]= (int) ldata[i];
1506 offset += 4;
1509 else
1511 *actual_size_ret *= *actual_format_ret / 8;
1512 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1513 offset += *actual_size_ret;
1516 /* This was allocated by Xlib, so use XFree. */
1517 XFree ((char *) tmp_data);
1520 XFlush (display);
1521 UNBLOCK_INPUT;
1522 *bytes_ret = offset;
1525 /* Use xfree, not XFree, to free the data obtained with this function. */
1527 static void
1528 receive_incremental_selection (Display *display, Window window, Atom property,
1529 Lisp_Object target_type,
1530 unsigned int min_size_bytes,
1531 unsigned char **data_ret, int *size_bytes_ret,
1532 Atom *type_ret, int *format_ret,
1533 unsigned long *size_ret)
1535 int offset = 0;
1536 struct prop_location *wait_object;
1537 *size_bytes_ret = min_size_bytes;
1538 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1540 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1542 /* At this point, we have read an INCR property.
1543 Delete the property to ack it.
1544 (But first, prepare to receive the next event in this handshake.)
1546 Now, we must loop, waiting for the sending window to put a value on
1547 that property, then reading the property, then deleting it to ack.
1548 We are done when the sender places a property of length 0.
1550 BLOCK_INPUT;
1551 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1552 TRACE1 (" Delete property %s",
1553 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1554 XDeleteProperty (display, window, property);
1555 TRACE1 (" Expect new value of property %s",
1556 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1557 wait_object = expect_property_change (display, window, property,
1558 PropertyNewValue);
1559 XFlush (display);
1560 UNBLOCK_INPUT;
1562 while (1)
1564 unsigned char *tmp_data;
1565 int tmp_size_bytes;
1567 TRACE0 (" Wait for property change");
1568 wait_for_property_change (wait_object);
1570 /* expect it again immediately, because x_get_window_property may
1571 .. no it won't, I don't get it.
1572 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1573 TRACE0 (" Get property value");
1574 x_get_window_property (display, window, property,
1575 &tmp_data, &tmp_size_bytes,
1576 type_ret, format_ret, size_ret, 1);
1578 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1580 if (tmp_size_bytes == 0) /* we're done */
1582 TRACE0 ("Done reading incrementally");
1584 if (! waiting_for_other_props_on_window (display, window))
1585 XSelectInput (display, window, STANDARD_EVENT_SET);
1586 /* Use xfree, not XFree, because x_get_window_property
1587 calls xmalloc itself. */
1588 xfree (tmp_data);
1589 break;
1592 BLOCK_INPUT;
1593 TRACE1 (" ACK by deleting property %s",
1594 XGetAtomName (display, property));
1595 XDeleteProperty (display, window, property);
1596 wait_object = expect_property_change (display, window, property,
1597 PropertyNewValue);
1598 XFlush (display);
1599 UNBLOCK_INPUT;
1601 if (*size_bytes_ret < offset + tmp_size_bytes)
1603 *size_bytes_ret = offset + tmp_size_bytes;
1604 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1607 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1608 offset += tmp_size_bytes;
1610 /* Use xfree, not XFree, because x_get_window_property
1611 calls xmalloc itself. */
1612 xfree (tmp_data);
1617 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1618 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1619 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1621 static Lisp_Object
1622 x_get_window_property_as_lisp_data (Display *display, Window window,
1623 Atom property,
1624 Lisp_Object target_type,
1625 Atom selection_atom)
1627 Atom actual_type;
1628 int actual_format;
1629 unsigned long actual_size;
1630 unsigned char *data = 0;
1631 int bytes = 0;
1632 Lisp_Object val;
1633 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1635 TRACE0 ("Reading selection data");
1637 x_get_window_property (display, window, property, &data, &bytes,
1638 &actual_type, &actual_format, &actual_size, 1);
1639 if (! data)
1641 int there_is_a_selection_owner;
1642 BLOCK_INPUT;
1643 there_is_a_selection_owner
1644 = XGetSelectionOwner (display, selection_atom);
1645 UNBLOCK_INPUT;
1646 if (there_is_a_selection_owner)
1647 signal_error ("Selection owner couldn't convert",
1648 actual_type
1649 ? list2 (target_type,
1650 x_atom_to_symbol (display, actual_type))
1651 : target_type);
1652 else
1653 signal_error ("No selection",
1654 x_atom_to_symbol (display, selection_atom));
1657 if (actual_type == dpyinfo->Xatom_INCR)
1659 /* That wasn't really the data, just the beginning. */
1661 unsigned int min_size_bytes = * ((unsigned int *) data);
1662 BLOCK_INPUT;
1663 /* Use xfree, not XFree, because x_get_window_property
1664 calls xmalloc itself. */
1665 xfree ((char *) data);
1666 UNBLOCK_INPUT;
1667 receive_incremental_selection (display, window, property, target_type,
1668 min_size_bytes, &data, &bytes,
1669 &actual_type, &actual_format,
1670 &actual_size);
1673 BLOCK_INPUT;
1674 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1675 XDeleteProperty (display, window, property);
1676 XFlush (display);
1677 UNBLOCK_INPUT;
1679 /* It's been read. Now convert it to a lisp object in some semi-rational
1680 manner. */
1681 val = selection_data_to_lisp_data (display, data, bytes,
1682 actual_type, actual_format);
1684 /* Use xfree, not XFree, because x_get_window_property
1685 calls xmalloc itself. */
1686 xfree ((char *) data);
1687 return val;
1690 /* These functions convert from the selection data read from the server into
1691 something that we can use from Lisp, and vice versa.
1693 Type: Format: Size: Lisp Type:
1694 ----- ------- ----- -----------
1695 * 8 * String
1696 ATOM 32 1 Symbol
1697 ATOM 32 > 1 Vector of Symbols
1698 * 16 1 Integer
1699 * 16 > 1 Vector of Integers
1700 * 32 1 if <=16 bits: Integer
1701 if > 16 bits: Cons of top16, bot16
1702 * 32 > 1 Vector of the above
1704 When converting a Lisp number to C, it is assumed to be of format 16 if
1705 it is an integer, and of format 32 if it is a cons of two integers.
1707 When converting a vector of numbers from Lisp to C, it is assumed to be
1708 of format 16 if every element in the vector is an integer, and is assumed
1709 to be of format 32 if any element is a cons of two integers.
1711 When converting an object to C, it may be of the form (SYMBOL . <data>)
1712 where SYMBOL is what we should claim that the type is. Format and
1713 representation are as above.
1715 Important: When format is 32, data should contain an array of int,
1716 not an array of long as the X library returns. This makes a difference
1717 when sizeof(long) != sizeof(int). */
1721 static Lisp_Object
1722 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1723 int size, Atom type, int format)
1725 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1727 if (type == dpyinfo->Xatom_NULL)
1728 return QNULL;
1730 /* Convert any 8-bit data to a string, for compactness. */
1731 else if (format == 8)
1733 Lisp_Object str, lispy_type;
1735 str = make_unibyte_string ((char *) data, size);
1736 /* Indicate that this string is from foreign selection by a text
1737 property `foreign-selection' so that the caller of
1738 x-get-selection-internal (usually x-get-selection) can know
1739 that the string must be decode. */
1740 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1741 lispy_type = QCOMPOUND_TEXT;
1742 else if (type == dpyinfo->Xatom_UTF8_STRING)
1743 lispy_type = QUTF8_STRING;
1744 else
1745 lispy_type = QSTRING;
1746 Fput_text_property (make_number (0), make_number (size),
1747 Qforeign_selection, lispy_type, str);
1748 return str;
1750 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1751 a vector of symbols.
1753 else if (type == XA_ATOM)
1755 int i;
1756 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1757 But the callers of these function has made sure the data for
1758 format == 32 is an array of int. Thus, use int instead
1759 of Atom. */
1760 int *idata = (int *) data;
1762 if (size == sizeof (int))
1763 return x_atom_to_symbol (display, (Atom) idata[0]);
1764 else
1766 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1767 make_number (0));
1768 for (i = 0; i < size / sizeof (int); i++)
1769 Faset (v, make_number (i),
1770 x_atom_to_symbol (display, (Atom) idata[i]));
1771 return v;
1775 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1776 If the number is 32 bits and won't fit in a Lisp_Int,
1777 convert it to a cons of integers, 16 bits in each half.
1779 else if (format == 32 && size == sizeof (int))
1780 return long_to_cons (((unsigned int *) data) [0]);
1781 else if (format == 16 && size == sizeof (short))
1782 return make_number ((int) (((unsigned short *) data) [0]));
1784 /* Convert any other kind of data to a vector of numbers, represented
1785 as above (as an integer, or a cons of two 16 bit integers.)
1787 else if (format == 16)
1789 int i;
1790 Lisp_Object v;
1791 v = Fmake_vector (make_number (size / 2), make_number (0));
1792 for (i = 0; i < size / 2; i++)
1794 int j = (int) ((unsigned short *) data) [i];
1795 Faset (v, make_number (i), make_number (j));
1797 return v;
1799 else
1801 int i;
1802 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1803 for (i = 0; i < size / 4; i++)
1805 unsigned int j = ((unsigned int *) data) [i];
1806 Faset (v, make_number (i), long_to_cons (j));
1808 return v;
1813 /* Use xfree, not XFree, to free the data obtained with this function. */
1815 static void
1816 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1817 unsigned char **data_ret, Atom *type_ret,
1818 unsigned int *size_ret,
1819 int *format_ret, int *nofree_ret)
1821 Lisp_Object type = Qnil;
1822 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1824 *nofree_ret = 0;
1826 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1828 type = XCAR (obj);
1829 obj = XCDR (obj);
1830 if (CONSP (obj) && NILP (XCDR (obj)))
1831 obj = XCAR (obj);
1834 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1835 { /* This is not the same as declining */
1836 *format_ret = 32;
1837 *size_ret = 0;
1838 *data_ret = 0;
1839 type = QNULL;
1841 else if (STRINGP (obj))
1843 if (SCHARS (obj) < SBYTES (obj))
1844 /* OBJ is a multibyte string containing a non-ASCII char. */
1845 signal_error ("Non-ASCII string must be encoded in advance", obj);
1846 if (NILP (type))
1847 type = QSTRING;
1848 *format_ret = 8;
1849 *size_ret = SBYTES (obj);
1850 *data_ret = SDATA (obj);
1851 *nofree_ret = 1;
1853 else if (SYMBOLP (obj))
1855 *format_ret = 32;
1856 *size_ret = 1;
1857 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1858 (*data_ret) [sizeof (Atom)] = 0;
1859 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
1860 if (NILP (type)) type = QATOM;
1862 else if (INTEGERP (obj)
1863 && XINT (obj) < 0xFFFF
1864 && XINT (obj) > -0xFFFF)
1866 *format_ret = 16;
1867 *size_ret = 1;
1868 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1869 (*data_ret) [sizeof (short)] = 0;
1870 (*(short **) data_ret) [0] = (short) XINT (obj);
1871 if (NILP (type)) type = QINTEGER;
1873 else if (INTEGERP (obj)
1874 || (CONSP (obj) && INTEGERP (XCAR (obj))
1875 && (INTEGERP (XCDR (obj))
1876 || (CONSP (XCDR (obj))
1877 && INTEGERP (XCAR (XCDR (obj)))))))
1879 *format_ret = 32;
1880 *size_ret = 1;
1881 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1882 (*data_ret) [sizeof (long)] = 0;
1883 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1884 if (NILP (type)) type = QINTEGER;
1886 else if (VECTORP (obj))
1888 /* Lisp_Vectors may represent a set of ATOMs;
1889 a set of 16 or 32 bit INTEGERs;
1890 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1892 int i;
1894 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1895 /* This vector is an ATOM set */
1897 if (NILP (type)) type = QATOM;
1898 *size_ret = XVECTOR (obj)->size;
1899 *format_ret = 32;
1900 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1901 for (i = 0; i < *size_ret; i++)
1902 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1903 (*(Atom **) data_ret) [i]
1904 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
1905 else
1906 signal_error ("All elements of selection vector must have same type", obj);
1908 #if 0 /* #### MULTIPLE doesn't work yet */
1909 else if (VECTORP (XVECTOR (obj)->contents [0]))
1910 /* This vector is an ATOM_PAIR set */
1912 if (NILP (type)) type = QATOM_PAIR;
1913 *size_ret = XVECTOR (obj)->size;
1914 *format_ret = 32;
1915 *data_ret = (unsigned char *)
1916 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1917 for (i = 0; i < *size_ret; i++)
1918 if (VECTORP (XVECTOR (obj)->contents [i]))
1920 Lisp_Object pair = XVECTOR (obj)->contents [i];
1921 if (XVECTOR (pair)->size != 2)
1922 signal_error (
1923 "Elements of the vector must be vectors of exactly two elements",
1924 pair);
1926 (*(Atom **) data_ret) [i * 2]
1927 = symbol_to_x_atom (dpyinfo, display,
1928 XVECTOR (pair)->contents [0]);
1929 (*(Atom **) data_ret) [(i * 2) + 1]
1930 = symbol_to_x_atom (dpyinfo, display,
1931 XVECTOR (pair)->contents [1]);
1933 else
1934 signal_error ("All elements of the vector must be of the same type",
1935 obj);
1938 #endif
1939 else
1940 /* This vector is an INTEGER set, or something like it */
1942 int data_size = 2;
1943 *size_ret = XVECTOR (obj)->size;
1944 if (NILP (type)) type = QINTEGER;
1945 *format_ret = 16;
1946 for (i = 0; i < *size_ret; i++)
1947 if (CONSP (XVECTOR (obj)->contents [i]))
1948 *format_ret = 32;
1949 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1950 signal_error (/* Qselection_error */
1951 "Elements of selection vector must be integers or conses of integers",
1952 obj);
1954 /* Use sizeof(long) even if it is more than 32 bits. See comment
1955 in x_get_window_property and x_fill_property_data. */
1957 if (*format_ret == 32) data_size = sizeof(long);
1958 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1959 for (i = 0; i < *size_ret; i++)
1960 if (*format_ret == 32)
1961 (*((unsigned long **) data_ret)) [i]
1962 = cons_to_long (XVECTOR (obj)->contents [i]);
1963 else
1964 (*((unsigned short **) data_ret)) [i]
1965 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1968 else
1969 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1971 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
1974 static Lisp_Object
1975 clean_local_selection_data (Lisp_Object obj)
1977 if (CONSP (obj)
1978 && INTEGERP (XCAR (obj))
1979 && CONSP (XCDR (obj))
1980 && INTEGERP (XCAR (XCDR (obj)))
1981 && NILP (XCDR (XCDR (obj))))
1982 obj = Fcons (XCAR (obj), XCDR (obj));
1984 if (CONSP (obj)
1985 && INTEGERP (XCAR (obj))
1986 && INTEGERP (XCDR (obj)))
1988 if (XINT (XCAR (obj)) == 0)
1989 return XCDR (obj);
1990 if (XINT (XCAR (obj)) == -1)
1991 return make_number (- XINT (XCDR (obj)));
1993 if (VECTORP (obj))
1995 int i;
1996 int size = XVECTOR (obj)->size;
1997 Lisp_Object copy;
1998 if (size == 1)
1999 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
2000 copy = Fmake_vector (make_number (size), Qnil);
2001 for (i = 0; i < size; i++)
2002 XVECTOR (copy)->contents [i]
2003 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
2004 return copy;
2006 return obj;
2009 /* Called from XTread_socket to handle SelectionNotify events.
2010 If it's the selection we are waiting for, stop waiting
2011 by setting the car of reading_selection_reply to non-nil.
2012 We store t there if the reply is successful, lambda if not. */
2014 void
2015 x_handle_selection_notify (XSelectionEvent *event)
2017 if (event->requestor != reading_selection_window)
2018 return;
2019 if (event->selection != reading_which_selection)
2020 return;
2022 TRACE0 ("Received SelectionNotify");
2023 XSETCAR (reading_selection_reply,
2024 (event->property != 0 ? Qt : Qlambda));
2028 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
2029 Sx_own_selection_internal, 2, 2, 0,
2030 doc: /* Assert an X selection of the given TYPE with the given VALUE.
2031 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2032 \(Those are literal upper-case symbol names, since that's what X expects.)
2033 VALUE is typically a string, or a cons of two markers, but may be
2034 anything that the functions on `selection-converter-alist' know about. */)
2035 (Lisp_Object selection_name, Lisp_Object selection_value)
2037 check_x ();
2038 CHECK_SYMBOL (selection_name);
2039 if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil");
2040 x_own_selection (selection_name, selection_value);
2041 return selection_value;
2045 /* Request the selection value from the owner. If we are the owner,
2046 simply return our selection value. If we are not the owner, this
2047 will block until all of the data has arrived. */
2049 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
2050 Sx_get_selection_internal, 2, 3, 0,
2051 doc: /* Return text selected from some X window.
2052 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2053 \(Those are literal upper-case symbol names, since that's what X expects.)
2054 TYPE is the type of data desired, typically `STRING'.
2055 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2056 selections. If omitted, defaults to the time for the last event. */)
2057 (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
2059 Lisp_Object val = Qnil;
2060 struct gcpro gcpro1, gcpro2;
2061 GCPRO2 (target_type, val); /* we store newly consed data into these */
2062 check_x ();
2063 CHECK_SYMBOL (selection_symbol);
2065 #if 0 /* #### MULTIPLE doesn't work yet */
2066 if (CONSP (target_type)
2067 && XCAR (target_type) == QMULTIPLE)
2069 CHECK_VECTOR (XCDR (target_type));
2070 /* So we don't destructively modify this... */
2071 target_type = copy_multiple_data (target_type);
2073 else
2074 #endif
2075 CHECK_SYMBOL (target_type);
2077 val = x_get_local_selection (selection_symbol, target_type, 1);
2079 if (NILP (val))
2081 val = x_get_foreign_selection (selection_symbol, target_type, time_stamp);
2082 goto DONE;
2085 if (CONSP (val)
2086 && SYMBOLP (XCAR (val)))
2088 val = XCDR (val);
2089 if (CONSP (val) && NILP (XCDR (val)))
2090 val = XCAR (val);
2092 val = clean_local_selection_data (val);
2093 DONE:
2094 UNGCPRO;
2095 return val;
2098 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2099 Sx_disown_selection_internal, 1, 2, 0,
2100 doc: /* If we own the selection SELECTION, disown it.
2101 Disowning it means there is no such selection. */)
2102 (Lisp_Object selection, Lisp_Object time)
2104 Time timestamp;
2105 Atom selection_atom;
2106 union {
2107 struct selection_input_event sie;
2108 struct input_event ie;
2109 } event;
2110 Display *display;
2111 struct x_display_info *dpyinfo;
2112 struct frame *sf = SELECTED_FRAME ();
2114 check_x ();
2115 if (! FRAME_X_P (sf))
2116 return Qnil;
2118 display = FRAME_X_DISPLAY (sf);
2119 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2120 CHECK_SYMBOL (selection);
2121 if (NILP (time))
2122 timestamp = last_event_timestamp;
2123 else
2124 timestamp = cons_to_long (time);
2126 if (NILP (assq_no_quit (selection, Vselection_alist)))
2127 return Qnil; /* Don't disown the selection when we're not the owner. */
2129 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
2131 BLOCK_INPUT;
2132 XSetSelectionOwner (display, selection_atom, None, timestamp);
2133 UNBLOCK_INPUT;
2135 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2136 generated for a window which owns the selection when that window sets
2137 the selection owner to None. The NCD server does, the MIT Sun4 server
2138 doesn't. So we synthesize one; this means we might get two, but
2139 that's ok, because the second one won't have any effect. */
2140 SELECTION_EVENT_DISPLAY (&event.sie) = display;
2141 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2142 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2143 x_handle_selection_clear (&event.ie);
2145 return Qt;
2148 /* Get rid of all the selections in buffer BUFFER.
2149 This is used when we kill a buffer. */
2151 void
2152 x_disown_buffer_selections (Lisp_Object buffer)
2154 Lisp_Object tail;
2155 struct buffer *buf = XBUFFER (buffer);
2157 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
2159 Lisp_Object elt, value;
2160 elt = XCAR (tail);
2161 value = XCDR (elt);
2162 if (CONSP (value) && MARKERP (XCAR (value))
2163 && XMARKER (XCAR (value))->buffer == buf)
2164 Fx_disown_selection_internal (XCAR (elt), Qnil);
2168 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2169 0, 1, 0,
2170 doc: /* Whether the current Emacs process owns the given X Selection.
2171 The arg should be the name of the selection in question, typically one of
2172 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2173 \(Those are literal upper-case symbol names, since that's what X expects.)
2174 For convenience, the symbol nil is the same as `PRIMARY',
2175 and t is the same as `SECONDARY'. */)
2176 (Lisp_Object selection)
2178 check_x ();
2179 CHECK_SYMBOL (selection);
2180 if (EQ (selection, Qnil)) selection = QPRIMARY;
2181 if (EQ (selection, Qt)) selection = QSECONDARY;
2183 if (NILP (Fassq (selection, Vselection_alist)))
2184 return Qnil;
2185 return Qt;
2188 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2189 0, 1, 0,
2190 doc: /* Whether there is an owner for the given X Selection.
2191 The arg should be the name of the selection in question, typically one of
2192 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2193 \(Those are literal upper-case symbol names, since that's what X expects.)
2194 For convenience, the symbol nil is the same as `PRIMARY',
2195 and t is the same as `SECONDARY'. */)
2196 (Lisp_Object selection)
2198 Window owner;
2199 Atom atom;
2200 Display *dpy;
2201 struct frame *sf = SELECTED_FRAME ();
2203 /* It should be safe to call this before we have an X frame. */
2204 if (! FRAME_X_P (sf))
2205 return Qnil;
2207 dpy = FRAME_X_DISPLAY (sf);
2208 CHECK_SYMBOL (selection);
2209 if (!NILP (Fx_selection_owner_p (selection)))
2210 return Qt;
2211 if (EQ (selection, Qnil)) selection = QPRIMARY;
2212 if (EQ (selection, Qt)) selection = QSECONDARY;
2213 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
2214 if (atom == 0)
2215 return Qnil;
2216 BLOCK_INPUT;
2217 owner = XGetSelectionOwner (dpy, atom);
2218 UNBLOCK_INPUT;
2219 return (owner ? Qt : Qnil);
2223 /***********************************************************************
2224 Drag and drop support
2225 ***********************************************************************/
2226 /* Check that lisp values are of correct type for x_fill_property_data.
2227 That is, number, string or a cons with two numbers (low and high 16
2228 bit parts of a 32 bit number). */
2231 x_check_property_data (Lisp_Object data)
2233 Lisp_Object iter;
2234 int size = 0;
2236 for (iter = data; CONSP (iter) && size != -1; iter = XCDR (iter), ++size)
2238 Lisp_Object o = XCAR (iter);
2240 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2241 size = -1;
2242 else if (CONSP (o) &&
2243 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2244 size = -1;
2247 return size;
2250 /* Convert lisp values to a C array. Values may be a number, a string
2251 which is taken as an X atom name and converted to the atom value, or
2252 a cons containing the two 16 bit parts of a 32 bit number.
2254 DPY is the display use to look up X atoms.
2255 DATA is a Lisp list of values to be converted.
2256 RET is the C array that contains the converted values. It is assumed
2257 it is big enough to hold all values.
2258 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2259 be stored in RET. Note that long is used for 32 even if long is more
2260 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2261 XClientMessageEvent). */
2263 void
2264 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2266 long val;
2267 long *d32 = (long *) ret;
2268 short *d16 = (short *) ret;
2269 char *d08 = (char *) ret;
2270 Lisp_Object iter;
2272 for (iter = data; CONSP (iter); iter = XCDR (iter))
2274 Lisp_Object o = XCAR (iter);
2276 if (INTEGERP (o))
2277 val = (long) XFASTINT (o);
2278 else if (FLOATP (o))
2279 val = (long) XFLOAT_DATA (o);
2280 else if (CONSP (o))
2281 val = (long) cons_to_long (o);
2282 else if (STRINGP (o))
2284 BLOCK_INPUT;
2285 val = (long) XInternAtom (dpy, (char *) SDATA (o), False);
2286 UNBLOCK_INPUT;
2288 else
2289 error ("Wrong type, must be string, number or cons");
2291 if (format == 8)
2292 *d08++ = (char) val;
2293 else if (format == 16)
2294 *d16++ = (short) val;
2295 else
2296 *d32++ = val;
2300 /* Convert an array of C values to a Lisp list.
2301 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2302 DATA is a C array of values to be converted.
2303 TYPE is the type of the data. Only XA_ATOM is special, it converts
2304 each number in DATA to its corresponfing X atom as a symbol.
2305 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2306 be stored in RET.
2307 SIZE is the number of elements in DATA.
2309 Important: When format is 32, data should contain an array of int,
2310 not an array of long as the X library returns. This makes a difference
2311 when sizeof(long) != sizeof(int).
2313 Also see comment for selection_data_to_lisp_data above. */
2315 Lisp_Object
2316 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2317 Atom type, int format, long unsigned int size)
2319 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2320 data, size*format/8, type, format);
2323 /* Get the mouse position in frame relative coordinates. */
2325 static void
2326 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2328 Window root, dummy_window;
2329 int dummy;
2331 BLOCK_INPUT;
2333 XQueryPointer (FRAME_X_DISPLAY (f),
2334 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2336 /* The root window which contains the pointer. */
2337 &root,
2339 /* Window pointer is on, not used */
2340 &dummy_window,
2342 /* The position on that root window. */
2343 x, y,
2345 /* x/y in dummy_window coordinates, not used. */
2346 &dummy, &dummy,
2348 /* Modifier keys and pointer buttons, about which
2349 we don't care. */
2350 (unsigned int *) &dummy);
2353 /* Absolute to relative. */
2354 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2355 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2357 UNBLOCK_INPUT;
2360 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2361 Sx_get_atom_name, 1, 2, 0,
2362 doc: /* Return the X atom name for VALUE as a string.
2363 VALUE may be a number or a cons where the car is the upper 16 bits and
2364 the cdr is the lower 16 bits of a 32 bit value.
2365 Use the display for FRAME or the current frame if FRAME is not given or nil.
2367 If the value is 0 or the atom is not known, return the empty string. */)
2368 (Lisp_Object value, Lisp_Object frame)
2370 struct frame *f = check_x_frame (frame);
2371 char *name = 0;
2372 char empty[] = "";
2373 Lisp_Object ret = Qnil;
2374 Display *dpy = FRAME_X_DISPLAY (f);
2375 Atom atom;
2376 int had_errors;
2378 if (INTEGERP (value))
2379 atom = (Atom) XUINT (value);
2380 else if (FLOATP (value))
2381 atom = (Atom) XFLOAT_DATA (value);
2382 else if (CONSP (value))
2383 atom = (Atom) cons_to_long (value);
2384 else
2385 error ("Wrong type, value must be number or cons");
2387 BLOCK_INPUT;
2388 x_catch_errors (dpy);
2389 name = atom ? XGetAtomName (dpy, atom) : empty;
2390 had_errors = x_had_errors_p (dpy);
2391 x_uncatch_errors ();
2393 if (!had_errors)
2394 ret = make_string (name, strlen (name));
2396 if (atom && name) XFree (name);
2397 if (NILP (ret)) ret = empty_unibyte_string;
2399 UNBLOCK_INPUT;
2401 return ret;
2404 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2405 Sx_register_dnd_atom, 1, 2, 0,
2406 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2407 ATOM can be a symbol or a string. The ATOM is interned on the display that
2408 FRAME is on. If FRAME is nil, the selected frame is used. */)
2409 (Lisp_Object atom, Lisp_Object frame)
2411 Atom x_atom;
2412 struct frame *f = check_x_frame (frame);
2413 size_t i;
2414 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2417 if (SYMBOLP (atom))
2418 x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom);
2419 else if (STRINGP (atom))
2421 BLOCK_INPUT;
2422 x_atom = XInternAtom (FRAME_X_DISPLAY (f), (char *) SDATA (atom), False);
2423 UNBLOCK_INPUT;
2425 else
2426 error ("ATOM must be a symbol or a string");
2428 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2429 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2430 return Qnil;
2432 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2434 dpyinfo->x_dnd_atoms_size *= 2;
2435 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2436 sizeof (*dpyinfo->x_dnd_atoms)
2437 * dpyinfo->x_dnd_atoms_size);
2440 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2441 return Qnil;
2444 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2447 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2449 Lisp_Object vec;
2450 Lisp_Object frame;
2451 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2452 unsigned long size = 160/event->format;
2453 int x, y;
2454 unsigned char *data = (unsigned char *) event->data.b;
2455 int idata[5];
2456 size_t i;
2458 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2459 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2461 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2463 XSETFRAME (frame, f);
2465 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2466 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2467 function expects them to be of size int (i.e. 32). So to be able to
2468 use that function, put the data in the form it expects if format is 32. */
2470 if (event->format == 32 && event->format < BITS_PER_LONG)
2472 int i;
2473 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2474 idata[i] = (int) event->data.l[i];
2475 data = (unsigned char *) idata;
2478 vec = Fmake_vector (make_number (4), Qnil);
2479 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2480 event->message_type)));
2481 ASET (vec, 1, frame);
2482 ASET (vec, 2, make_number (event->format));
2483 ASET (vec, 3, x_property_data_to_lisp (f,
2484 data,
2485 event->message_type,
2486 event->format,
2487 size));
2489 mouse_position_for_drop (f, &x, &y);
2490 bufp->kind = DRAG_N_DROP_EVENT;
2491 bufp->frame_or_window = frame;
2492 bufp->timestamp = CurrentTime;
2493 bufp->x = make_number (x);
2494 bufp->y = make_number (y);
2495 bufp->arg = vec;
2496 bufp->modifiers = 0;
2498 return 1;
2501 DEFUN ("x-send-client-message", Fx_send_client_event,
2502 Sx_send_client_message, 6, 6, 0,
2503 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2505 For DISPLAY, specify either a frame or a display name (a string).
2506 If DISPLAY is nil, that stands for the selected frame's display.
2507 DEST may be a number, in which case it is a Window id. The value 0 may
2508 be used to send to the root window of the DISPLAY.
2509 If DEST is a cons, it is converted to a 32 bit number
2510 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2511 number is then used as a window id.
2512 If DEST is a frame the event is sent to the outer window of that frame.
2513 A value of nil means the currently selected frame.
2514 If DEST is the string "PointerWindow" the event is sent to the window that
2515 contains the pointer. If DEST is the string "InputFocus" the event is
2516 sent to the window that has the input focus.
2517 FROM is the frame sending the event. Use nil for currently selected frame.
2518 MESSAGE-TYPE is the name of an Atom as a string.
2519 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2520 bits. VALUES is a list of numbers, cons and/or strings containing the values
2521 to send. If a value is a string, it is converted to an Atom and the value of
2522 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2523 with the high 16 bits from the car and the lower 16 bit from the cdr.
2524 If more values than fits into the event is given, the excessive values
2525 are ignored. */)
2526 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2528 struct x_display_info *dpyinfo = check_x_display_info (display);
2530 CHECK_STRING (message_type);
2531 x_send_client_event(display, dest, from,
2532 XInternAtom (dpyinfo->display,
2533 SDATA (message_type),
2534 False),
2535 format, values);
2537 return Qnil;
2540 void
2541 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2543 struct x_display_info *dpyinfo = check_x_display_info (display);
2544 Window wdest;
2545 XEvent event;
2546 struct frame *f = check_x_frame (from);
2547 int to_root;
2549 CHECK_NUMBER (format);
2550 CHECK_CONS (values);
2552 if (x_check_property_data (values) == -1)
2553 error ("Bad data in VALUES, must be number, cons or string");
2555 event.xclient.type = ClientMessage;
2556 event.xclient.format = XFASTINT (format);
2558 if (event.xclient.format != 8 && event.xclient.format != 16
2559 && event.xclient.format != 32)
2560 error ("FORMAT must be one of 8, 16 or 32");
2562 if (FRAMEP (dest) || NILP (dest))
2564 struct frame *fdest = check_x_frame (dest);
2565 wdest = FRAME_OUTER_WINDOW (fdest);
2567 else if (STRINGP (dest))
2569 if (strcmp (SDATA (dest), "PointerWindow") == 0)
2570 wdest = PointerWindow;
2571 else if (strcmp (SDATA (dest), "InputFocus") == 0)
2572 wdest = InputFocus;
2573 else
2574 error ("DEST as a string must be one of PointerWindow or InputFocus");
2576 else if (INTEGERP (dest))
2577 wdest = (Window) XFASTINT (dest);
2578 else if (FLOATP (dest))
2579 wdest = (Window) XFLOAT_DATA (dest);
2580 else if (CONSP (dest))
2582 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2583 error ("Both car and cdr for DEST must be numbers");
2584 else
2585 wdest = (Window) cons_to_long (dest);
2587 else
2588 error ("DEST must be a frame, nil, string, number or cons");
2590 if (wdest == 0) wdest = dpyinfo->root_window;
2591 to_root = wdest == dpyinfo->root_window;
2593 BLOCK_INPUT;
2595 event.xclient.message_type = message_type;
2596 event.xclient.display = dpyinfo->display;
2598 /* Some clients (metacity for example) expects sending window to be here
2599 when sending to the root window. */
2600 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2603 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2604 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2605 event.xclient.format);
2607 /* If event mask is 0 the event is sent to the client that created
2608 the destination window. But if we are sending to the root window,
2609 there is no such client. Then we set the event mask to 0xffff. The
2610 event then goes to clients selecting for events on the root window. */
2611 x_catch_errors (dpyinfo->display);
2613 int propagate = to_root ? False : True;
2614 unsigned mask = to_root ? 0xffff : 0;
2615 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2616 XFlush (dpyinfo->display);
2618 x_uncatch_errors ();
2619 UNBLOCK_INPUT;
2623 void
2624 syms_of_xselect (void)
2626 defsubr (&Sx_get_selection_internal);
2627 defsubr (&Sx_own_selection_internal);
2628 defsubr (&Sx_disown_selection_internal);
2629 defsubr (&Sx_selection_owner_p);
2630 defsubr (&Sx_selection_exists_p);
2632 defsubr (&Sx_get_atom_name);
2633 defsubr (&Sx_send_client_message);
2634 defsubr (&Sx_register_dnd_atom);
2636 reading_selection_reply = Fcons (Qnil, Qnil);
2637 staticpro (&reading_selection_reply);
2638 reading_selection_window = 0;
2639 reading_which_selection = 0;
2641 property_change_wait_list = 0;
2642 prop_location_identifier = 0;
2643 property_change_reply = Fcons (Qnil, Qnil);
2644 staticpro (&property_change_reply);
2646 Vselection_alist = Qnil;
2647 staticpro (&Vselection_alist);
2649 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
2650 doc: /* An alist associating X Windows selection-types with functions.
2651 These functions are called to convert the selection, with three args:
2652 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2653 a desired type to which the selection should be converted;
2654 and the local selection value (whatever was given to `x-own-selection').
2656 The function should return the value to send to the X server
2657 \(typically a string). A return value of nil
2658 means that the conversion could not be done.
2659 A return value which is the symbol `NULL'
2660 means that a side-effect was executed,
2661 and there is no meaningful selection value. */);
2662 Vselection_converter_alist = Qnil;
2664 DEFVAR_LISP ("x-lost-selection-functions", &Vx_lost_selection_functions,
2665 doc: /* A list of functions to be called when Emacs loses an X selection.
2666 \(This happens when some other X client makes its own selection
2667 or when a Lisp program explicitly clears the selection.)
2668 The functions are called with one argument, the selection type
2669 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2670 Vx_lost_selection_functions = Qnil;
2672 DEFVAR_LISP ("x-sent-selection-functions", &Vx_sent_selection_functions,
2673 doc: /* A list of functions to be called when Emacs answers a selection request.
2674 The functions are called with four arguments:
2675 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2676 - the selection-type which Emacs was asked to convert the
2677 selection into before sending (for example, `STRING' or `LENGTH');
2678 - a flag indicating success or failure for responding to the request.
2679 We might have failed (and declined the request) for any number of reasons,
2680 including being asked for a selection that we no longer own, or being asked
2681 to convert into a type that we don't know about or that is inappropriate.
2682 This hook doesn't let you change the behavior of Emacs's selection replies,
2683 it merely informs you that they have happened. */);
2684 Vx_sent_selection_functions = Qnil;
2686 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
2687 doc: /* Number of milliseconds to wait for a selection reply.
2688 If the selection owner doesn't reply in this time, we give up.
2689 A value of 0 means wait as long as necessary. This is initialized from the
2690 \"*selectionTimeout\" resource. */);
2691 x_selection_timeout = 0;
2693 /* QPRIMARY is defined in keyboard.c. */
2694 QSECONDARY = intern_c_string ("SECONDARY"); staticpro (&QSECONDARY);
2695 QSTRING = intern_c_string ("STRING"); staticpro (&QSTRING);
2696 QINTEGER = intern_c_string ("INTEGER"); staticpro (&QINTEGER);
2697 QCLIPBOARD = intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD);
2698 QTIMESTAMP = intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2699 QTEXT = intern_c_string ("TEXT"); staticpro (&QTEXT);
2700 QCOMPOUND_TEXT = intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
2701 QUTF8_STRING = intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING);
2702 QDELETE = intern_c_string ("DELETE"); staticpro (&QDELETE);
2703 QMULTIPLE = intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE);
2704 QINCR = intern_c_string ("INCR"); staticpro (&QINCR);
2705 QEMACS_TMP = intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
2706 QTARGETS = intern_c_string ("TARGETS"); staticpro (&QTARGETS);
2707 QATOM = intern_c_string ("ATOM"); staticpro (&QATOM);
2708 QATOM_PAIR = intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
2709 QNULL = intern_c_string ("NULL"); staticpro (&QNULL);
2710 Qcompound_text_with_extensions = intern_c_string ("compound-text-with-extensions");
2711 staticpro (&Qcompound_text_with_extensions);
2713 Qforeign_selection = intern_c_string ("foreign-selection");
2714 staticpro (&Qforeign_selection);
2717 /* arch-tag: 7c293b0f-9918-4f69-8ac7-03e142307236
2718 (do not change this comment) */