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