Mention `delete-frame-functions' replacing `delete-frame-hook',
[emacs.git] / src / xselect.c
blob07a61ca2de3b2a013fea6ab73894f501ff1f7f89
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001
3 Free Software Foundation.
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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Rewritten by jwz */
25 #include <config.h>
26 #include "lisp.h"
27 #include "xterm.h" /* for all of the X includes */
28 #include "dispextern.h" /* frame.h seems to want this */
29 #include "frame.h" /* Need this to get the X window of selected_frame */
30 #include "blockinput.h"
31 #include "buffer.h"
32 #include "charset.h"
33 #include "coding.h"
34 #include "process.h"
35 #include "composite.h"
37 struct prop_location;
39 static Lisp_Object x_atom_to_symbol P_ ((Display *dpy, Atom atom));
40 static Atom symbol_to_x_atom P_ ((struct x_display_info *, Display *,
41 Lisp_Object));
42 static void x_own_selection P_ ((Lisp_Object, Lisp_Object));
43 static Lisp_Object x_get_local_selection P_ ((Lisp_Object, Lisp_Object, int));
44 static void x_decline_selection_request P_ ((struct input_event *));
45 static Lisp_Object x_selection_request_lisp_error P_ ((Lisp_Object));
46 static Lisp_Object queue_selection_requests_unwind P_ ((Lisp_Object));
47 static Lisp_Object some_frame_on_display P_ ((struct x_display_info *));
48 static void x_reply_selection_request P_ ((struct input_event *, int,
49 unsigned char *, int, Atom));
50 static int waiting_for_other_props_on_window P_ ((Display *, Window));
51 static struct prop_location *expect_property_change P_ ((Display *, Window,
52 Atom, int));
53 static void unexpect_property_change P_ ((struct prop_location *));
54 static Lisp_Object wait_for_property_change_unwind P_ ((Lisp_Object));
55 static void wait_for_property_change P_ ((struct prop_location *));
56 static Lisp_Object x_get_foreign_selection P_ ((Lisp_Object, Lisp_Object));
57 static void x_get_window_property P_ ((Display *, Window, Atom,
58 unsigned char **, int *,
59 Atom *, int *, unsigned long *, int));
60 static void receive_incremental_selection P_ ((Display *, Window, Atom,
61 Lisp_Object, unsigned,
62 unsigned char **, int *,
63 Atom *, int *, unsigned long *));
64 static Lisp_Object x_get_window_property_as_lisp_data P_ ((Display *,
65 Window, Atom,
66 Lisp_Object, Atom));
67 static Lisp_Object selection_data_to_lisp_data P_ ((Display *, unsigned char *,
68 int, Atom, int));
69 static void lisp_data_to_selection_data P_ ((Display *, Lisp_Object,
70 unsigned char **, Atom *,
71 unsigned *, int *, int *));
72 static Lisp_Object clean_local_selection_data P_ ((Lisp_Object));
73 static void initialize_cut_buffers P_ ((Display *, Window));
76 /* Printing traces to stderr. */
78 #ifdef TRACE_SELECTION
79 #define TRACE0(fmt) \
80 fprintf (stderr, "%d: " fmt "\n", getpid ())
81 #define TRACE1(fmt, a0) \
82 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
83 #define TRACE2(fmt, a0, a1) \
84 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
85 #else
86 #define TRACE0(fmt) (void) 0
87 #define TRACE1(fmt, a0) (void) 0
88 #define TRACE2(fmt, a0, a1) (void) 0
89 #endif
92 #define CUT_BUFFER_SUPPORT
94 Lisp_Object QPRIMARY, QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
95 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
96 QATOM_PAIR;
98 Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
99 Lisp_Object QUTF8_STRING; /* This is a type of selection. */
101 Lisp_Object Qcompound_text_with_extensions;
103 #ifdef CUT_BUFFER_SUPPORT
104 Lisp_Object QCUT_BUFFER0, QCUT_BUFFER1, QCUT_BUFFER2, QCUT_BUFFER3,
105 QCUT_BUFFER4, QCUT_BUFFER5, QCUT_BUFFER6, QCUT_BUFFER7;
106 #endif
108 static Lisp_Object Vx_lost_selection_hooks;
109 static Lisp_Object Vx_sent_selection_hooks;
110 /* Coding system for communicating with other X clients via cutbuffer,
111 selection, and clipboard. */
112 static Lisp_Object Vselection_coding_system;
114 /* Coding system for the next communicating with other X clients. */
115 static Lisp_Object Vnext_selection_coding_system;
117 /* If this is a smaller number than the max-request-size of the display,
118 emacs will use INCR selection transfer when the selection is larger
119 than this. The max-request-size is usually around 64k, so if you want
120 emacs to use incremental selection transfers when the selection is
121 smaller than that, set this. I added this mostly for debugging the
122 incremental transfer stuff, but it might improve server performance. */
123 #define MAX_SELECTION_QUANTUM 0xFFFFFF
125 #ifdef HAVE_X11R4
126 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
127 #else
128 #define SELECTION_QUANTUM(dpy) (((dpy)->max_request_size << 2) - 100)
129 #endif
131 /* The timestamp of the last input event Emacs received from the X server. */
132 /* Defined in keyboard.c. */
133 extern unsigned long last_event_timestamp;
135 /* This is an association list whose elements are of the form
136 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
137 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
138 SELECTION-VALUE is the value that emacs owns for that selection.
139 It may be any kind of Lisp object.
140 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
141 as a cons of two 16-bit numbers (making a 32 bit time.)
142 FRAME is the frame for which we made the selection.
143 If there is an entry in this alist, then it can be assumed that Emacs owns
144 that selection.
145 The only (eq) parts of this list that are visible from Lisp are the
146 selection-values. */
147 static Lisp_Object Vselection_alist;
149 /* This is an alist whose CARs are selection-types (whose names are the same
150 as the names of X Atoms) and whose CDRs are the names of Lisp functions to
151 call to convert the given Emacs selection value to a string representing
152 the given selection type. This is for Lisp-level extension of the emacs
153 selection handling. */
154 static Lisp_Object Vselection_converter_alist;
156 /* If the selection owner takes too long to reply to a selection request,
157 we give up on it. This is in milliseconds (0 = no timeout.) */
158 static EMACS_INT x_selection_timeout;
160 /* Utility functions */
162 static void lisp_data_to_selection_data ();
163 static Lisp_Object selection_data_to_lisp_data ();
164 static Lisp_Object x_get_window_property_as_lisp_data ();
166 /* This converts a Lisp symbol to a server Atom, avoiding a server
167 roundtrip whenever possible. */
169 static Atom
170 symbol_to_x_atom (dpyinfo, display, sym)
171 struct x_display_info *dpyinfo;
172 Display *display;
173 Lisp_Object sym;
175 Atom val;
176 if (NILP (sym)) return 0;
177 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
178 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
179 if (EQ (sym, QSTRING)) return XA_STRING;
180 if (EQ (sym, QINTEGER)) return XA_INTEGER;
181 if (EQ (sym, QATOM)) return XA_ATOM;
182 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
183 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
184 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
185 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
186 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
187 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
188 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
189 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
190 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
191 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
192 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
193 #ifdef CUT_BUFFER_SUPPORT
194 if (EQ (sym, QCUT_BUFFER0)) return XA_CUT_BUFFER0;
195 if (EQ (sym, QCUT_BUFFER1)) return XA_CUT_BUFFER1;
196 if (EQ (sym, QCUT_BUFFER2)) return XA_CUT_BUFFER2;
197 if (EQ (sym, QCUT_BUFFER3)) return XA_CUT_BUFFER3;
198 if (EQ (sym, QCUT_BUFFER4)) return XA_CUT_BUFFER4;
199 if (EQ (sym, QCUT_BUFFER5)) return XA_CUT_BUFFER5;
200 if (EQ (sym, QCUT_BUFFER6)) return XA_CUT_BUFFER6;
201 if (EQ (sym, QCUT_BUFFER7)) return XA_CUT_BUFFER7;
202 #endif
203 if (!SYMBOLP (sym)) abort ();
205 TRACE1 (" XInternAtom %s", (char *) SDATA (SYMBOL_NAME (sym)));
206 BLOCK_INPUT;
207 val = XInternAtom (display, (char *) SDATA (SYMBOL_NAME (sym)), False);
208 UNBLOCK_INPUT;
209 return val;
213 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
214 and calls to intern whenever possible. */
216 static Lisp_Object
217 x_atom_to_symbol (dpy, atom)
218 Display *dpy;
219 Atom atom;
221 struct x_display_info *dpyinfo;
222 char *str;
223 Lisp_Object val;
225 if (! atom)
226 return Qnil;
228 switch (atom)
230 case XA_PRIMARY:
231 return QPRIMARY;
232 case XA_SECONDARY:
233 return QSECONDARY;
234 case XA_STRING:
235 return QSTRING;
236 case XA_INTEGER:
237 return QINTEGER;
238 case XA_ATOM:
239 return QATOM;
240 #ifdef CUT_BUFFER_SUPPORT
241 case XA_CUT_BUFFER0:
242 return QCUT_BUFFER0;
243 case XA_CUT_BUFFER1:
244 return QCUT_BUFFER1;
245 case XA_CUT_BUFFER2:
246 return QCUT_BUFFER2;
247 case XA_CUT_BUFFER3:
248 return QCUT_BUFFER3;
249 case XA_CUT_BUFFER4:
250 return QCUT_BUFFER4;
251 case XA_CUT_BUFFER5:
252 return QCUT_BUFFER5;
253 case XA_CUT_BUFFER6:
254 return QCUT_BUFFER6;
255 case XA_CUT_BUFFER7:
256 return QCUT_BUFFER7;
257 #endif
260 dpyinfo = x_display_info_for_display (dpy);
261 if (atom == dpyinfo->Xatom_CLIPBOARD)
262 return QCLIPBOARD;
263 if (atom == dpyinfo->Xatom_TIMESTAMP)
264 return QTIMESTAMP;
265 if (atom == dpyinfo->Xatom_TEXT)
266 return QTEXT;
267 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
268 return QCOMPOUND_TEXT;
269 if (atom == dpyinfo->Xatom_UTF8_STRING)
270 return QUTF8_STRING;
271 if (atom == dpyinfo->Xatom_DELETE)
272 return QDELETE;
273 if (atom == dpyinfo->Xatom_MULTIPLE)
274 return QMULTIPLE;
275 if (atom == dpyinfo->Xatom_INCR)
276 return QINCR;
277 if (atom == dpyinfo->Xatom_EMACS_TMP)
278 return QEMACS_TMP;
279 if (atom == dpyinfo->Xatom_TARGETS)
280 return QTARGETS;
281 if (atom == dpyinfo->Xatom_NULL)
282 return QNULL;
284 BLOCK_INPUT;
285 str = XGetAtomName (dpy, atom);
286 UNBLOCK_INPUT;
287 TRACE1 ("XGetAtomName --> %s", str);
288 if (! str) return Qnil;
289 val = intern (str);
290 BLOCK_INPUT;
291 /* This was allocated by Xlib, so use XFree. */
292 XFree (str);
293 UNBLOCK_INPUT;
294 return val;
297 /* Do protocol to assert ourself as a selection owner.
298 Update the Vselection_alist so that we can reply to later requests for
299 our selection. */
301 static void
302 x_own_selection (selection_name, selection_value)
303 Lisp_Object selection_name, selection_value;
305 struct frame *sf = SELECTED_FRAME ();
306 Window selecting_window = FRAME_X_WINDOW (sf);
307 Display *display = FRAME_X_DISPLAY (sf);
308 Time time = last_event_timestamp;
309 Atom selection_atom;
310 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
311 int count;
313 CHECK_SYMBOL (selection_name);
314 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
316 BLOCK_INPUT;
317 count = x_catch_errors (display);
318 XSetSelectionOwner (display, selection_atom, selecting_window, time);
319 x_check_errors (display, "Can't set selection: %s");
320 x_uncatch_errors (display, count);
321 UNBLOCK_INPUT;
323 /* Now update the local cache */
325 Lisp_Object selection_time;
326 Lisp_Object selection_data;
327 Lisp_Object prev_value;
329 selection_time = long_to_cons ((unsigned long) time);
330 selection_data = Fcons (selection_name,
331 Fcons (selection_value,
332 Fcons (selection_time,
333 Fcons (selected_frame, Qnil))));
334 prev_value = assq_no_quit (selection_name, Vselection_alist);
336 Vselection_alist = Fcons (selection_data, Vselection_alist);
338 /* If we already owned the selection, remove the old selection data.
339 Perhaps we should destructively modify it instead.
340 Don't use Fdelq as that may QUIT. */
341 if (!NILP (prev_value))
343 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
344 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
345 if (EQ (prev_value, Fcar (XCDR (rest))))
347 XSETCDR (rest, Fcdr (XCDR (rest)));
348 break;
354 /* Given a selection-name and desired type, look up our local copy of
355 the selection value and convert it to the type.
356 The value is nil or a string.
357 This function is used both for remote requests (LOCAL_REQUEST is zero)
358 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
360 This calls random Lisp code, and may signal or gc. */
362 static Lisp_Object
363 x_get_local_selection (selection_symbol, target_type, local_request)
364 Lisp_Object selection_symbol, target_type;
365 int local_request;
367 Lisp_Object local_value;
368 Lisp_Object handler_fn, value, type, check;
369 int count;
371 local_value = assq_no_quit (selection_symbol, Vselection_alist);
373 if (NILP (local_value)) return Qnil;
375 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
376 if (EQ (target_type, QTIMESTAMP))
378 handler_fn = Qnil;
379 value = XCAR (XCDR (XCDR (local_value)));
381 #if 0
382 else if (EQ (target_type, QDELETE))
384 handler_fn = Qnil;
385 Fx_disown_selection_internal
386 (selection_symbol,
387 XCAR (XCDR (XCDR (local_value))));
388 value = QNULL;
390 #endif
392 #if 0 /* #### MULTIPLE doesn't work yet */
393 else if (CONSP (target_type)
394 && XCAR (target_type) == QMULTIPLE)
396 Lisp_Object pairs;
397 int size;
398 int i;
399 pairs = XCDR (target_type);
400 size = XVECTOR (pairs)->size;
401 /* If the target is MULTIPLE, then target_type looks like
402 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
403 We modify the second element of each pair in the vector and
404 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
406 for (i = 0; i < size; i++)
408 Lisp_Object pair;
409 pair = XVECTOR (pairs)->contents [i];
410 XVECTOR (pair)->contents [1]
411 = x_get_local_selection (XVECTOR (pair)->contents [0],
412 XVECTOR (pair)->contents [1],
413 local_request);
415 return pairs;
417 #endif
418 else
420 /* Don't allow a quit within the converter.
421 When the user types C-g, he would be surprised
422 if by luck it came during a converter. */
423 count = SPECPDL_INDEX ();
424 specbind (Qinhibit_quit, Qt);
426 CHECK_SYMBOL (target_type);
427 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
428 if (!NILP (handler_fn))
429 value = call3 (handler_fn,
430 selection_symbol, (local_request ? Qnil : target_type),
431 XCAR (XCDR (local_value)));
432 else
433 value = Qnil;
434 unbind_to (count, Qnil);
437 /* Make sure this value is of a type that we could transmit
438 to another X client. */
440 check = value;
441 if (CONSP (value)
442 && SYMBOLP (XCAR (value)))
443 type = XCAR (value),
444 check = XCDR (value);
446 if (STRINGP (check)
447 || VECTORP (check)
448 || SYMBOLP (check)
449 || INTEGERP (check)
450 || NILP (value))
451 return value;
452 /* Check for a value that cons_to_long could handle. */
453 else if (CONSP (check)
454 && INTEGERP (XCAR (check))
455 && (INTEGERP (XCDR (check))
457 (CONSP (XCDR (check))
458 && INTEGERP (XCAR (XCDR (check)))
459 && NILP (XCDR (XCDR (check))))))
460 return value;
461 else
462 return
463 Fsignal (Qerror,
464 Fcons (build_string ("invalid data returned by selection-conversion function"),
465 Fcons (handler_fn, Fcons (value, Qnil))));
468 /* Subroutines of x_reply_selection_request. */
470 /* Send a SelectionNotify event to the requestor with property=None,
471 meaning we were unable to do what they wanted. */
473 static void
474 x_decline_selection_request (event)
475 struct input_event *event;
477 XSelectionEvent reply;
478 int count;
480 reply.type = SelectionNotify;
481 reply.display = SELECTION_EVENT_DISPLAY (event);
482 reply.requestor = SELECTION_EVENT_REQUESTOR (event);
483 reply.selection = SELECTION_EVENT_SELECTION (event);
484 reply.time = SELECTION_EVENT_TIME (event);
485 reply.target = SELECTION_EVENT_TARGET (event);
486 reply.property = None;
488 /* The reason for the error may be that the receiver has
489 died in the meantime. Handle that case. */
490 BLOCK_INPUT;
491 count = x_catch_errors (reply.display);
492 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
493 XFlush (reply.display);
494 x_uncatch_errors (reply.display, count);
495 UNBLOCK_INPUT;
498 /* This is the selection request currently being processed.
499 It is set to zero when the request is fully processed. */
500 static struct input_event *x_selection_current_request;
502 /* Display info in x_selection_request. */
504 static struct x_display_info *selection_request_dpyinfo;
506 /* Used as an unwind-protect clause so that, if a selection-converter signals
507 an error, we tell the requester that we were unable to do what they wanted
508 before we throw to top-level or go into the debugger or whatever. */
510 static Lisp_Object
511 x_selection_request_lisp_error (ignore)
512 Lisp_Object ignore;
514 if (x_selection_current_request != 0
515 && selection_request_dpyinfo->display)
516 x_decline_selection_request (x_selection_current_request);
517 return Qnil;
521 /* This stuff is so that INCR selections are reentrant (that is, so we can
522 be servicing multiple INCR selection requests simultaneously.) I haven't
523 actually tested that yet. */
525 /* Keep a list of the property changes that are awaited. */
527 struct prop_location
529 int identifier;
530 Display *display;
531 Window window;
532 Atom property;
533 int desired_state;
534 int arrived;
535 struct prop_location *next;
538 static struct prop_location *expect_property_change ();
539 static void wait_for_property_change ();
540 static void unexpect_property_change ();
541 static int waiting_for_other_props_on_window ();
543 static int prop_location_identifier;
545 static Lisp_Object property_change_reply;
547 static struct prop_location *property_change_reply_object;
549 static struct prop_location *property_change_wait_list;
551 static Lisp_Object
552 queue_selection_requests_unwind (frame)
553 Lisp_Object frame;
555 FRAME_PTR f = XFRAME (frame);
557 if (! NILP (frame))
558 x_stop_queuing_selection_requests (FRAME_X_DISPLAY (f));
559 return Qnil;
562 /* Return some frame whose display info is DPYINFO.
563 Return nil if there is none. */
565 static Lisp_Object
566 some_frame_on_display (dpyinfo)
567 struct x_display_info *dpyinfo;
569 Lisp_Object list, frame;
571 FOR_EACH_FRAME (list, frame)
573 if (FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
574 return frame;
577 return Qnil;
580 /* Send the reply to a selection request event EVENT.
581 TYPE is the type of selection data requested.
582 DATA and SIZE describe the data to send, already converted.
583 FORMAT is the unit-size (in bits) of the data to be transmitted. */
585 static void
586 x_reply_selection_request (event, format, data, size, type)
587 struct input_event *event;
588 int format, size;
589 unsigned char *data;
590 Atom type;
592 XSelectionEvent reply;
593 Display *display = SELECTION_EVENT_DISPLAY (event);
594 Window window = SELECTION_EVENT_REQUESTOR (event);
595 int bytes_remaining;
596 int format_bytes = format/8;
597 int max_bytes = SELECTION_QUANTUM (display);
598 struct x_display_info *dpyinfo = x_display_info_for_display (display);
599 int count;
601 if (max_bytes > MAX_SELECTION_QUANTUM)
602 max_bytes = MAX_SELECTION_QUANTUM;
604 reply.type = SelectionNotify;
605 reply.display = display;
606 reply.requestor = window;
607 reply.selection = SELECTION_EVENT_SELECTION (event);
608 reply.time = SELECTION_EVENT_TIME (event);
609 reply.target = SELECTION_EVENT_TARGET (event);
610 reply.property = SELECTION_EVENT_PROPERTY (event);
611 if (reply.property == None)
612 reply.property = reply.target;
614 /* #### XChangeProperty can generate BadAlloc, and we must handle it! */
615 BLOCK_INPUT;
616 count = x_catch_errors (display);
618 /* Store the data on the requested property.
619 If the selection is large, only store the first N bytes of it.
621 bytes_remaining = size * format_bytes;
622 if (bytes_remaining <= max_bytes)
624 /* Send all the data at once, with minimal handshaking. */
625 TRACE1 ("Sending all %d bytes", bytes_remaining);
626 XChangeProperty (display, window, reply.property, type, format,
627 PropModeReplace, data, size);
628 /* At this point, the selection was successfully stored; ack it. */
629 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
631 else
633 /* Send an INCR selection. */
634 struct prop_location *wait_object;
635 int had_errors;
636 Lisp_Object frame;
638 frame = some_frame_on_display (dpyinfo);
640 /* If the display no longer has frames, we can't expect
641 to get many more selection requests from it, so don't
642 bother trying to queue them. */
643 if (!NILP (frame))
645 x_start_queuing_selection_requests (display);
647 record_unwind_protect (queue_selection_requests_unwind,
648 frame);
651 if (x_window_to_frame (dpyinfo, window)) /* #### debug */
652 error ("Attempt to transfer an INCR to ourself!");
654 TRACE2 ("Start sending %d bytes incrementally (%s)",
655 bytes_remaining, XGetAtomName (display, reply.property));
656 wait_object = expect_property_change (display, window, reply.property,
657 PropertyDelete);
659 TRACE1 ("Set %s to number of bytes to send",
660 XGetAtomName (display, reply.property));
661 XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
662 32, PropModeReplace,
663 (unsigned char *) &bytes_remaining, 1);
664 XSelectInput (display, window, PropertyChangeMask);
666 /* Tell 'em the INCR data is there... */
667 TRACE0 ("Send SelectionNotify event");
668 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
669 XFlush (display);
671 had_errors = x_had_errors_p (display);
672 UNBLOCK_INPUT;
674 /* First, wait for the requester to ack by deleting the property.
675 This can run random lisp code (process handlers) or signal. */
676 if (! had_errors)
678 TRACE1 ("Waiting for ACK (deletion of %s)",
679 XGetAtomName (display, reply.property));
680 wait_for_property_change (wait_object);
683 TRACE0 ("Got ACK");
684 while (bytes_remaining)
686 int i = ((bytes_remaining < max_bytes)
687 ? bytes_remaining
688 : max_bytes);
690 BLOCK_INPUT;
692 wait_object
693 = expect_property_change (display, window, reply.property,
694 PropertyDelete);
696 TRACE1 ("Sending increment of %d bytes", i);
697 TRACE1 ("Set %s to increment data",
698 XGetAtomName (display, reply.property));
700 /* Append the next chunk of data to the property. */
701 XChangeProperty (display, window, reply.property, type, format,
702 PropModeAppend, data, i / format_bytes);
703 bytes_remaining -= i;
704 data += i;
705 XFlush (display);
706 had_errors = x_had_errors_p (display);
707 UNBLOCK_INPUT;
709 if (had_errors)
710 break;
712 /* Now wait for the requester to ack this chunk by deleting the
713 property. This can run random lisp code or signal. */
714 TRACE1 ("Waiting for increment ACK (deletion of %s)",
715 XGetAtomName (display, reply.property));
716 wait_for_property_change (wait_object);
719 /* Now write a zero-length chunk to the property to tell the
720 requester that we're done. */
721 BLOCK_INPUT;
722 if (! waiting_for_other_props_on_window (display, window))
723 XSelectInput (display, window, 0L);
725 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
726 XGetAtomName (display, reply.property));
727 XChangeProperty (display, window, reply.property, type, format,
728 PropModeReplace, data, 0);
729 TRACE0 ("Done sending incrementally");
732 /* The window we're communicating with may have been deleted
733 in the meantime (that's a real situation from a bug report).
734 In this case, there may be events in the event queue still
735 refering to the deleted window, and we'll get a BadWindow error
736 in XTread_socket when processing the events. I don't have
737 an idea how to fix that. gerd, 2001-01-98. */
738 XFlush (display);
739 x_uncatch_errors (display, count);
740 UNBLOCK_INPUT;
743 /* Handle a SelectionRequest event EVENT.
744 This is called from keyboard.c when such an event is found in the queue. */
746 void
747 x_handle_selection_request (event)
748 struct input_event *event;
750 struct gcpro gcpro1, gcpro2, gcpro3;
751 Lisp_Object local_selection_data;
752 Lisp_Object selection_symbol;
753 Lisp_Object target_symbol;
754 Lisp_Object converted_selection;
755 Time local_selection_time;
756 Lisp_Object successful_p;
757 int count;
758 struct x_display_info *dpyinfo
759 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
761 local_selection_data = Qnil;
762 target_symbol = Qnil;
763 converted_selection = Qnil;
764 successful_p = Qnil;
766 GCPRO3 (local_selection_data, converted_selection, target_symbol);
768 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
769 SELECTION_EVENT_SELECTION (event));
771 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
773 if (NILP (local_selection_data))
775 /* Someone asked for the selection, but we don't have it any more.
777 x_decline_selection_request (event);
778 goto DONE;
781 local_selection_time = (Time)
782 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
784 if (SELECTION_EVENT_TIME (event) != CurrentTime
785 && local_selection_time > SELECTION_EVENT_TIME (event))
787 /* Someone asked for the selection, and we have one, but not the one
788 they're looking for.
790 x_decline_selection_request (event);
791 goto DONE;
794 x_selection_current_request = event;
795 count = SPECPDL_INDEX ();
796 selection_request_dpyinfo = dpyinfo;
797 record_unwind_protect (x_selection_request_lisp_error, Qnil);
799 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
800 SELECTION_EVENT_TARGET (event));
802 #if 0 /* #### MULTIPLE doesn't work yet */
803 if (EQ (target_symbol, QMULTIPLE))
804 target_symbol = fetch_multiple_target (event);
805 #endif
807 /* Convert lisp objects back into binary data */
809 converted_selection
810 = x_get_local_selection (selection_symbol, target_symbol, 0);
812 if (! NILP (converted_selection))
814 unsigned char *data;
815 unsigned int size;
816 int format;
817 Atom type;
818 int nofree;
820 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
821 converted_selection,
822 &data, &type, &size, &format, &nofree);
824 x_reply_selection_request (event, format, data, size, type);
825 successful_p = Qt;
827 /* Indicate we have successfully processed this event. */
828 x_selection_current_request = 0;
830 /* Use xfree, not XFree, because lisp_data_to_selection_data
831 calls xmalloc itself. */
832 if (!nofree)
833 xfree (data);
835 unbind_to (count, Qnil);
837 DONE:
839 UNGCPRO;
841 /* Let random lisp code notice that the selection has been asked for. */
843 Lisp_Object rest;
844 rest = Vx_sent_selection_hooks;
845 if (!EQ (rest, Qunbound))
846 for (; CONSP (rest); rest = Fcdr (rest))
847 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
851 /* Handle a SelectionClear event EVENT, which indicates that some
852 client cleared out our previously asserted selection.
853 This is called from keyboard.c when such an event is found in the queue. */
855 void
856 x_handle_selection_clear (event)
857 struct input_event *event;
859 Display *display = SELECTION_EVENT_DISPLAY (event);
860 Atom selection = SELECTION_EVENT_SELECTION (event);
861 Time changed_owner_time = SELECTION_EVENT_TIME (event);
863 Lisp_Object selection_symbol, local_selection_data;
864 Time local_selection_time;
865 struct x_display_info *dpyinfo = x_display_info_for_display (display);
866 struct x_display_info *t_dpyinfo;
868 /* If the new selection owner is also Emacs,
869 don't clear the new selection. */
870 BLOCK_INPUT;
871 /* Check each display on the same terminal,
872 to see if this Emacs job now owns the selection
873 through that display. */
874 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
875 if (t_dpyinfo->kboard == dpyinfo->kboard)
877 Window owner_window
878 = XGetSelectionOwner (t_dpyinfo->display, selection);
879 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
881 UNBLOCK_INPUT;
882 return;
885 UNBLOCK_INPUT;
887 selection_symbol = x_atom_to_symbol (display, selection);
889 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
891 /* Well, we already believe that we don't own it, so that's just fine. */
892 if (NILP (local_selection_data)) return;
894 local_selection_time = (Time)
895 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
897 /* This SelectionClear is for a selection that we no longer own, so we can
898 disregard it. (That is, we have reasserted the selection since this
899 request was generated.) */
901 if (changed_owner_time != CurrentTime
902 && local_selection_time > changed_owner_time)
903 return;
905 /* Otherwise, we're really honest and truly being told to drop it.
906 Don't use Fdelq as that may QUIT;. */
908 if (EQ (local_selection_data, Fcar (Vselection_alist)))
909 Vselection_alist = Fcdr (Vselection_alist);
910 else
912 Lisp_Object rest;
913 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
914 if (EQ (local_selection_data, Fcar (XCDR (rest))))
916 XSETCDR (rest, Fcdr (XCDR (rest)));
917 break;
921 /* Let random lisp code notice that the selection has been stolen. */
924 Lisp_Object rest;
925 rest = Vx_lost_selection_hooks;
926 if (!EQ (rest, Qunbound))
928 for (; CONSP (rest); rest = Fcdr (rest))
929 call1 (Fcar (rest), selection_symbol);
930 prepare_menu_bars ();
931 redisplay_preserve_echo_area (20);
936 /* Clear all selections that were made from frame F.
937 We do this when about to delete a frame. */
939 void
940 x_clear_frame_selections (f)
941 FRAME_PTR f;
943 Lisp_Object frame;
944 Lisp_Object rest;
946 XSETFRAME (frame, f);
948 /* Otherwise, we're really honest and truly being told to drop it.
949 Don't use Fdelq as that may QUIT;. */
951 /* Delete elements from the beginning of Vselection_alist. */
952 while (!NILP (Vselection_alist)
953 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
955 /* Let random Lisp code notice that the selection has been stolen. */
956 Lisp_Object hooks, selection_symbol;
958 hooks = Vx_lost_selection_hooks;
959 selection_symbol = Fcar (Fcar (Vselection_alist));
961 if (!EQ (hooks, Qunbound))
963 for (; CONSP (hooks); hooks = Fcdr (hooks))
964 call1 (Fcar (hooks), selection_symbol);
965 #if 0 /* This can crash when deleting a frame
966 from x_connection_closed. Anyway, it seems unnecessary;
967 something else should cause a redisplay. */
968 redisplay_preserve_echo_area (21);
969 #endif
972 Vselection_alist = Fcdr (Vselection_alist);
975 /* Delete elements after the beginning of Vselection_alist. */
976 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
977 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
979 /* Let random Lisp code notice that the selection has been stolen. */
980 Lisp_Object hooks, selection_symbol;
982 hooks = Vx_lost_selection_hooks;
983 selection_symbol = Fcar (Fcar (XCDR (rest)));
985 if (!EQ (hooks, Qunbound))
987 for (; CONSP (hooks); hooks = Fcdr (hooks))
988 call1 (Fcar (hooks), selection_symbol);
989 #if 0 /* See above */
990 redisplay_preserve_echo_area (22);
991 #endif
993 XSETCDR (rest, Fcdr (XCDR (rest)));
994 break;
998 /* Nonzero if any properties for DISPLAY and WINDOW
999 are on the list of what we are waiting for. */
1001 static int
1002 waiting_for_other_props_on_window (display, window)
1003 Display *display;
1004 Window window;
1006 struct prop_location *rest = property_change_wait_list;
1007 while (rest)
1008 if (rest->display == display && rest->window == window)
1009 return 1;
1010 else
1011 rest = rest->next;
1012 return 0;
1015 /* Add an entry to the list of property changes we are waiting for.
1016 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1017 The return value is a number that uniquely identifies
1018 this awaited property change. */
1020 static struct prop_location *
1021 expect_property_change (display, window, property, state)
1022 Display *display;
1023 Window window;
1024 Atom property;
1025 int state;
1027 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1028 pl->identifier = ++prop_location_identifier;
1029 pl->display = display;
1030 pl->window = window;
1031 pl->property = property;
1032 pl->desired_state = state;
1033 pl->next = property_change_wait_list;
1034 pl->arrived = 0;
1035 property_change_wait_list = pl;
1036 return pl;
1039 /* Delete an entry from the list of property changes we are waiting for.
1040 IDENTIFIER is the number that uniquely identifies the entry. */
1042 static void
1043 unexpect_property_change (location)
1044 struct prop_location *location;
1046 struct prop_location *prev = 0, *rest = property_change_wait_list;
1047 while (rest)
1049 if (rest == location)
1051 if (prev)
1052 prev->next = rest->next;
1053 else
1054 property_change_wait_list = rest->next;
1055 xfree (rest);
1056 return;
1058 prev = rest;
1059 rest = rest->next;
1063 /* Remove the property change expectation element for IDENTIFIER. */
1065 static Lisp_Object
1066 wait_for_property_change_unwind (identifierval)
1067 Lisp_Object identifierval;
1069 unexpect_property_change ((struct prop_location *)
1070 (XFASTINT (XCAR (identifierval)) << 16
1071 | XFASTINT (XCDR (identifierval))));
1072 return Qnil;
1075 /* Actually wait for a property change.
1076 IDENTIFIER should be the value that expect_property_change returned. */
1078 static void
1079 wait_for_property_change (location)
1080 struct prop_location *location;
1082 int secs, usecs;
1083 int count = SPECPDL_INDEX ();
1084 Lisp_Object tem;
1086 tem = Fcons (Qnil, Qnil);
1087 XSETCARFASTINT (tem, (EMACS_UINT)location >> 16);
1088 XSETCDRFASTINT (tem, (EMACS_UINT)location & 0xffff);
1090 /* Make sure to do unexpect_property_change if we quit or err. */
1091 record_unwind_protect (wait_for_property_change_unwind, tem);
1093 XSETCAR (property_change_reply, Qnil);
1095 property_change_reply_object = location;
1096 /* If the event we are waiting for arrives beyond here, it will set
1097 property_change_reply, because property_change_reply_object says so. */
1098 if (! location->arrived)
1100 secs = x_selection_timeout / 1000;
1101 usecs = (x_selection_timeout % 1000) * 1000;
1102 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1103 wait_reading_process_input (secs, usecs, property_change_reply, 0);
1105 if (NILP (XCAR (property_change_reply)))
1107 TRACE0 (" Timed out");
1108 error ("Timed out waiting for property-notify event");
1112 unbind_to (count, Qnil);
1115 /* Called from XTread_socket in response to a PropertyNotify event. */
1117 void
1118 x_handle_property_notify (event)
1119 XPropertyEvent *event;
1121 struct prop_location *prev = 0, *rest = property_change_wait_list;
1123 while (rest)
1125 if (rest->property == event->atom
1126 && rest->window == event->window
1127 && rest->display == event->display
1128 && rest->desired_state == event->state)
1130 TRACE2 ("Expected %s of property %s",
1131 (event->state == PropertyDelete ? "deletion" : "change"),
1132 XGetAtomName (event->display, event->atom));
1134 rest->arrived = 1;
1136 /* If this is the one wait_for_property_change is waiting for,
1137 tell it to wake up. */
1138 if (rest == property_change_reply_object)
1139 XSETCAR (property_change_reply, Qt);
1141 if (prev)
1142 prev->next = rest->next;
1143 else
1144 property_change_wait_list = rest->next;
1145 xfree (rest);
1146 return;
1149 prev = rest;
1150 rest = rest->next;
1156 #if 0 /* #### MULTIPLE doesn't work yet */
1158 static Lisp_Object
1159 fetch_multiple_target (event)
1160 XSelectionRequestEvent *event;
1162 Display *display = event->display;
1163 Window window = event->requestor;
1164 Atom target = event->target;
1165 Atom selection_atom = event->selection;
1166 int result;
1168 return
1169 Fcons (QMULTIPLE,
1170 x_get_window_property_as_lisp_data (display, window, target,
1171 QMULTIPLE, selection_atom));
1174 static Lisp_Object
1175 copy_multiple_data (obj)
1176 Lisp_Object obj;
1178 Lisp_Object vec;
1179 int i;
1180 int size;
1181 if (CONSP (obj))
1182 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1184 CHECK_VECTOR (obj);
1185 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1186 for (i = 0; i < size; i++)
1188 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1189 CHECK_VECTOR (vec2);
1190 if (XVECTOR (vec2)->size != 2)
1191 /* ??? Confusing error message */
1192 Fsignal (Qerror, Fcons (build_string ("vectors must be of length 2"),
1193 Fcons (vec2, Qnil)));
1194 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1195 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1196 = XVECTOR (vec2)->contents [0];
1197 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1198 = XVECTOR (vec2)->contents [1];
1200 return vec;
1203 #endif
1206 /* Variables for communication with x_handle_selection_notify. */
1207 static Atom reading_which_selection;
1208 static Lisp_Object reading_selection_reply;
1209 static Window reading_selection_window;
1211 /* Do protocol to read selection-data from the server.
1212 Converts this to Lisp data and returns it. */
1214 static Lisp_Object
1215 x_get_foreign_selection (selection_symbol, target_type)
1216 Lisp_Object selection_symbol, target_type;
1218 struct frame *sf = SELECTED_FRAME ();
1219 Window requestor_window = FRAME_X_WINDOW (sf);
1220 Display *display = FRAME_X_DISPLAY (sf);
1221 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1222 Time requestor_time = last_event_timestamp;
1223 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1224 Atom selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1225 Atom type_atom;
1226 int secs, usecs;
1227 int count;
1228 Lisp_Object frame;
1230 if (CONSP (target_type))
1231 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1232 else
1233 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1235 BLOCK_INPUT;
1237 count = x_catch_errors (display);
1239 TRACE2 ("Get selection %s, type %s",
1240 XGetAtomName (display, type_atom),
1241 XGetAtomName (display, target_property));
1243 XConvertSelection (display, selection_atom, type_atom, target_property,
1244 requestor_window, requestor_time);
1245 XFlush (display);
1247 /* Prepare to block until the reply has been read. */
1248 reading_selection_window = requestor_window;
1249 reading_which_selection = selection_atom;
1250 XSETCAR (reading_selection_reply, Qnil);
1252 frame = some_frame_on_display (dpyinfo);
1254 /* If the display no longer has frames, we can't expect
1255 to get many more selection requests from it, so don't
1256 bother trying to queue them. */
1257 if (!NILP (frame))
1259 x_start_queuing_selection_requests (display);
1261 record_unwind_protect (queue_selection_requests_unwind,
1262 frame);
1264 UNBLOCK_INPUT;
1266 /* This allows quits. Also, don't wait forever. */
1267 secs = x_selection_timeout / 1000;
1268 usecs = (x_selection_timeout % 1000) * 1000;
1269 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1270 wait_reading_process_input (secs, usecs, reading_selection_reply, 0);
1271 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1273 BLOCK_INPUT;
1274 x_check_errors (display, "Cannot get selection: %s");
1275 x_uncatch_errors (display, count);
1276 UNBLOCK_INPUT;
1278 if (NILP (XCAR (reading_selection_reply)))
1279 error ("Timed out waiting for reply from selection owner");
1280 if (EQ (XCAR (reading_selection_reply), Qlambda))
1281 error ("No `%s' selection", SDATA (SYMBOL_NAME (selection_symbol)));
1283 /* Otherwise, the selection is waiting for us on the requested property. */
1284 return
1285 x_get_window_property_as_lisp_data (display, requestor_window,
1286 target_property, target_type,
1287 selection_atom);
1290 /* Subroutines of x_get_window_property_as_lisp_data */
1292 /* Use xfree, not XFree, to free the data obtained with this function. */
1294 static void
1295 x_get_window_property (display, window, property, data_ret, bytes_ret,
1296 actual_type_ret, actual_format_ret, actual_size_ret,
1297 delete_p)
1298 Display *display;
1299 Window window;
1300 Atom property;
1301 unsigned char **data_ret;
1302 int *bytes_ret;
1303 Atom *actual_type_ret;
1304 int *actual_format_ret;
1305 unsigned long *actual_size_ret;
1306 int delete_p;
1308 int total_size;
1309 unsigned long bytes_remaining;
1310 int offset = 0;
1311 unsigned char *tmp_data = 0;
1312 int result;
1313 int buffer_size = SELECTION_QUANTUM (display);
1315 if (buffer_size > MAX_SELECTION_QUANTUM)
1316 buffer_size = MAX_SELECTION_QUANTUM;
1318 BLOCK_INPUT;
1320 /* First probe the thing to find out how big it is. */
1321 result = XGetWindowProperty (display, window, property,
1322 0L, 0L, False, AnyPropertyType,
1323 actual_type_ret, actual_format_ret,
1324 actual_size_ret,
1325 &bytes_remaining, &tmp_data);
1326 if (result != Success)
1328 UNBLOCK_INPUT;
1329 *data_ret = 0;
1330 *bytes_ret = 0;
1331 return;
1334 /* This was allocated by Xlib, so use XFree. */
1335 XFree ((char *) tmp_data);
1337 if (*actual_type_ret == None || *actual_format_ret == 0)
1339 UNBLOCK_INPUT;
1340 return;
1343 total_size = bytes_remaining + 1;
1344 *data_ret = (unsigned char *) xmalloc (total_size);
1346 /* Now read, until we've gotten it all. */
1347 while (bytes_remaining)
1349 #ifdef TRACE_SELECTION
1350 int last = bytes_remaining;
1351 #endif
1352 result
1353 = XGetWindowProperty (display, window, property,
1354 (long)offset/4, (long)buffer_size/4,
1355 False,
1356 AnyPropertyType,
1357 actual_type_ret, actual_format_ret,
1358 actual_size_ret, &bytes_remaining, &tmp_data);
1360 TRACE2 ("Read %ld bytes from property %s",
1361 last - bytes_remaining,
1362 XGetAtomName (display, property));
1364 /* If this doesn't return Success at this point, it means that
1365 some clod deleted the selection while we were in the midst of
1366 reading it. Deal with that, I guess.... */
1367 if (result != Success)
1368 break;
1369 *actual_size_ret *= *actual_format_ret / 8;
1370 bcopy (tmp_data, (*data_ret) + offset, *actual_size_ret);
1371 offset += *actual_size_ret;
1373 /* This was allocated by Xlib, so use XFree. */
1374 XFree ((char *) tmp_data);
1377 XFlush (display);
1378 UNBLOCK_INPUT;
1379 *bytes_ret = offset;
1382 /* Use xfree, not XFree, to free the data obtained with this function. */
1384 static void
1385 receive_incremental_selection (display, window, property, target_type,
1386 min_size_bytes, data_ret, size_bytes_ret,
1387 type_ret, format_ret, size_ret)
1388 Display *display;
1389 Window window;
1390 Atom property;
1391 Lisp_Object target_type; /* for error messages only */
1392 unsigned int min_size_bytes;
1393 unsigned char **data_ret;
1394 int *size_bytes_ret;
1395 Atom *type_ret;
1396 unsigned long *size_ret;
1397 int *format_ret;
1399 int offset = 0;
1400 struct prop_location *wait_object;
1401 *size_bytes_ret = min_size_bytes;
1402 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1404 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1406 /* At this point, we have read an INCR property.
1407 Delete the property to ack it.
1408 (But first, prepare to receive the next event in this handshake.)
1410 Now, we must loop, waiting for the sending window to put a value on
1411 that property, then reading the property, then deleting it to ack.
1412 We are done when the sender places a property of length 0.
1414 BLOCK_INPUT;
1415 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1416 TRACE1 (" Delete property %s",
1417 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
1418 XDeleteProperty (display, window, property);
1419 TRACE1 (" Expect new value of property %s",
1420 XSYMBOL (x_atom_to_symbol (display, property))->name->data);
1421 wait_object = expect_property_change (display, window, property,
1422 PropertyNewValue);
1423 XFlush (display);
1424 UNBLOCK_INPUT;
1426 while (1)
1428 unsigned char *tmp_data;
1429 int tmp_size_bytes;
1431 TRACE0 (" Wait for property change");
1432 wait_for_property_change (wait_object);
1434 /* expect it again immediately, because x_get_window_property may
1435 .. no it won't, I don't get it.
1436 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1437 TRACE0 (" Get property value");
1438 x_get_window_property (display, window, property,
1439 &tmp_data, &tmp_size_bytes,
1440 type_ret, format_ret, size_ret, 1);
1442 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1444 if (tmp_size_bytes == 0) /* we're done */
1446 TRACE0 ("Done reading incrementally");
1448 if (! waiting_for_other_props_on_window (display, window))
1449 XSelectInput (display, window, STANDARD_EVENT_SET);
1450 unexpect_property_change (wait_object);
1451 /* Use xfree, not XFree, because x_get_window_property
1452 calls xmalloc itself. */
1453 if (tmp_data) xfree (tmp_data);
1454 break;
1457 BLOCK_INPUT;
1458 TRACE1 (" ACK by deleting property %s",
1459 XGetAtomName (display, property));
1460 XDeleteProperty (display, window, property);
1461 wait_object = expect_property_change (display, window, property,
1462 PropertyNewValue);
1463 XFlush (display);
1464 UNBLOCK_INPUT;
1466 if (*size_bytes_ret < offset + tmp_size_bytes)
1468 *size_bytes_ret = offset + tmp_size_bytes;
1469 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1472 bcopy (tmp_data, (*data_ret) + offset, tmp_size_bytes);
1473 offset += tmp_size_bytes;
1475 /* Use xfree, not XFree, because x_get_window_property
1476 calls xmalloc itself. */
1477 xfree (tmp_data);
1482 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1483 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1484 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1486 static Lisp_Object
1487 x_get_window_property_as_lisp_data (display, window, property, target_type,
1488 selection_atom)
1489 Display *display;
1490 Window window;
1491 Atom property;
1492 Lisp_Object target_type; /* for error messages only */
1493 Atom selection_atom; /* for error messages only */
1495 Atom actual_type;
1496 int actual_format;
1497 unsigned long actual_size;
1498 unsigned char *data = 0;
1499 int bytes = 0;
1500 Lisp_Object val;
1501 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1503 TRACE0 ("Reading selection data");
1505 x_get_window_property (display, window, property, &data, &bytes,
1506 &actual_type, &actual_format, &actual_size, 1);
1507 if (! data)
1509 int there_is_a_selection_owner;
1510 BLOCK_INPUT;
1511 there_is_a_selection_owner
1512 = XGetSelectionOwner (display, selection_atom);
1513 UNBLOCK_INPUT;
1514 Fsignal (Qerror,
1515 there_is_a_selection_owner
1516 ? Fcons (build_string ("selection owner couldn't convert"),
1517 actual_type
1518 ? Fcons (target_type,
1519 Fcons (x_atom_to_symbol (display,
1520 actual_type),
1521 Qnil))
1522 : Fcons (target_type, Qnil))
1523 : Fcons (build_string ("no selection"),
1524 Fcons (x_atom_to_symbol (display,
1525 selection_atom),
1526 Qnil)));
1529 if (actual_type == dpyinfo->Xatom_INCR)
1531 /* That wasn't really the data, just the beginning. */
1533 unsigned int min_size_bytes = * ((unsigned int *) data);
1534 BLOCK_INPUT;
1535 /* Use xfree, not XFree, because x_get_window_property
1536 calls xmalloc itself. */
1537 xfree ((char *) data);
1538 UNBLOCK_INPUT;
1539 receive_incremental_selection (display, window, property, target_type,
1540 min_size_bytes, &data, &bytes,
1541 &actual_type, &actual_format,
1542 &actual_size);
1545 BLOCK_INPUT;
1546 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1547 XDeleteProperty (display, window, property);
1548 XFlush (display);
1549 UNBLOCK_INPUT;
1551 /* It's been read. Now convert it to a lisp object in some semi-rational
1552 manner. */
1553 val = selection_data_to_lisp_data (display, data, bytes,
1554 actual_type, actual_format);
1556 /* Use xfree, not XFree, because x_get_window_property
1557 calls xmalloc itself. */
1558 xfree ((char *) data);
1559 return val;
1562 /* These functions convert from the selection data read from the server into
1563 something that we can use from Lisp, and vice versa.
1565 Type: Format: Size: Lisp Type:
1566 ----- ------- ----- -----------
1567 * 8 * String
1568 ATOM 32 1 Symbol
1569 ATOM 32 > 1 Vector of Symbols
1570 * 16 1 Integer
1571 * 16 > 1 Vector of Integers
1572 * 32 1 if <=16 bits: Integer
1573 if > 16 bits: Cons of top16, bot16
1574 * 32 > 1 Vector of the above
1576 When converting a Lisp number to C, it is assumed to be of format 16 if
1577 it is an integer, and of format 32 if it is a cons of two integers.
1579 When converting a vector of numbers from Lisp to C, it is assumed to be
1580 of format 16 if every element in the vector is an integer, and is assumed
1581 to be of format 32 if any element is a cons of two integers.
1583 When converting an object to C, it may be of the form (SYMBOL . <data>)
1584 where SYMBOL is what we should claim that the type is. Format and
1585 representation are as above. */
1589 static Lisp_Object
1590 selection_data_to_lisp_data (display, data, size, type, format)
1591 Display *display;
1592 unsigned char *data;
1593 Atom type;
1594 int size, format;
1596 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1598 if (type == dpyinfo->Xatom_NULL)
1599 return QNULL;
1601 /* Convert any 8-bit data to a string, for compactness. */
1602 else if (format == 8)
1604 Lisp_Object str;
1605 int require_encoding = 0;
1607 if (
1608 #if 1
1610 #else
1611 ! NILP (buffer_defaults.enable_multibyte_characters)
1612 #endif
1615 /* If TYPE is `TEXT' or `COMPOUND_TEXT', we should decode
1616 DATA to Emacs internal format because DATA may be encoded
1617 in compound text format. In addtion, if TYPE is `STRING'
1618 and DATA contains any 8-bit Latin-1 code, we should also
1619 decode it. */
1620 if (type == dpyinfo->Xatom_TEXT
1621 || type == dpyinfo->Xatom_COMPOUND_TEXT)
1622 require_encoding = 1;
1623 else if (type == XA_STRING)
1625 int i;
1626 for (i = 0; i < size; i++)
1628 if (data[i] >= 0x80)
1630 require_encoding = 1;
1631 break;
1636 if (!require_encoding)
1638 str = make_unibyte_string ((char *) data, size);
1639 Vlast_coding_system_used = Qraw_text;
1641 else
1643 int bufsize;
1644 unsigned char *buf;
1645 struct coding_system coding;
1647 if (NILP (Vnext_selection_coding_system))
1648 Vnext_selection_coding_system = Vselection_coding_system;
1649 setup_coding_system
1650 (Fcheck_coding_system(Vnext_selection_coding_system), &coding);
1651 coding.src_multibyte = 0;
1652 coding.dst_multibyte = 1;
1653 Vnext_selection_coding_system = Qnil;
1654 coding.mode |= CODING_MODE_LAST_BLOCK;
1655 /* We explicitely disable composition handling because
1656 selection data should not contain any composition
1657 sequence. */
1658 coding.composing = COMPOSITION_DISABLED;
1659 bufsize = decoding_buffer_size (&coding, size);
1660 buf = (unsigned char *) xmalloc (bufsize);
1661 decode_coding (&coding, data, buf, size, bufsize);
1662 str = make_string_from_bytes ((char *) buf,
1663 coding.produced_char, coding.produced);
1664 xfree (buf);
1666 if (SYMBOLP (coding.post_read_conversion)
1667 && !NILP (Ffboundp (coding.post_read_conversion)))
1668 str = run_pre_post_conversion_on_str (str, &coding, 0);
1669 Vlast_coding_system_used = coding.symbol;
1671 compose_chars_in_text (0, SCHARS (str), str);
1672 return str;
1674 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1675 a vector of symbols.
1677 else if (type == XA_ATOM)
1679 int i;
1680 if (size == sizeof (Atom))
1681 return x_atom_to_symbol (display, *((Atom *) data));
1682 else
1684 Lisp_Object v = Fmake_vector (make_number (size / sizeof (Atom)),
1685 make_number (0));
1686 for (i = 0; i < size / sizeof (Atom); i++)
1687 Faset (v, make_number (i),
1688 x_atom_to_symbol (display, ((Atom *) data) [i]));
1689 return v;
1693 /* Convert a single 16 or small 32 bit number to a Lisp_Int.
1694 If the number is > 16 bits, convert it to a cons of integers,
1695 16 bits in each half.
1697 else if (format == 32 && size == sizeof (long))
1698 return long_to_cons (((unsigned long *) data) [0]);
1699 else if (format == 16 && size == sizeof (short))
1700 return make_number ((int) (((unsigned short *) data) [0]));
1702 /* Convert any other kind of data to a vector of numbers, represented
1703 as above (as an integer, or a cons of two 16 bit integers.)
1705 else if (format == 16)
1707 int i;
1708 Lisp_Object v;
1709 v = Fmake_vector (make_number (size / 2), make_number (0));
1710 for (i = 0; i < size / 2; i++)
1712 int j = (int) ((unsigned short *) data) [i];
1713 Faset (v, make_number (i), make_number (j));
1715 return v;
1717 else
1719 int i;
1720 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1721 for (i = 0; i < size / 4; i++)
1723 unsigned long j = ((unsigned long *) data) [i];
1724 Faset (v, make_number (i), long_to_cons (j));
1726 return v;
1731 /* Use xfree, not XFree, to free the data obtained with this function. */
1733 static void
1734 lisp_data_to_selection_data (display, obj,
1735 data_ret, type_ret, size_ret,
1736 format_ret, nofree_ret)
1737 Display *display;
1738 Lisp_Object obj;
1739 unsigned char **data_ret;
1740 Atom *type_ret;
1741 unsigned int *size_ret;
1742 int *format_ret;
1743 int *nofree_ret;
1745 Lisp_Object type = Qnil;
1746 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1748 *nofree_ret = 0;
1750 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1752 type = XCAR (obj);
1753 obj = XCDR (obj);
1754 if (CONSP (obj) && NILP (XCDR (obj)))
1755 obj = XCAR (obj);
1758 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1759 { /* This is not the same as declining */
1760 *format_ret = 32;
1761 *size_ret = 0;
1762 *data_ret = 0;
1763 type = QNULL;
1765 else if (STRINGP (obj))
1767 xassert (! STRING_MULTIBYTE (obj));
1768 if (NILP (type))
1769 type = QSTRING;
1770 *format_ret = 8;
1771 *size_ret = SBYTES (obj);
1772 *data_ret = SDATA (obj);
1773 *nofree_ret = 1;
1775 else if (SYMBOLP (obj))
1777 *format_ret = 32;
1778 *size_ret = 1;
1779 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1780 (*data_ret) [sizeof (Atom)] = 0;
1781 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
1782 if (NILP (type)) type = QATOM;
1784 else if (INTEGERP (obj)
1785 && XINT (obj) < 0xFFFF
1786 && XINT (obj) > -0xFFFF)
1788 *format_ret = 16;
1789 *size_ret = 1;
1790 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1791 (*data_ret) [sizeof (short)] = 0;
1792 (*(short **) data_ret) [0] = (short) XINT (obj);
1793 if (NILP (type)) type = QINTEGER;
1795 else if (INTEGERP (obj)
1796 || (CONSP (obj) && INTEGERP (XCAR (obj))
1797 && (INTEGERP (XCDR (obj))
1798 || (CONSP (XCDR (obj))
1799 && INTEGERP (XCAR (XCDR (obj)))))))
1801 *format_ret = 32;
1802 *size_ret = 1;
1803 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1804 (*data_ret) [sizeof (long)] = 0;
1805 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1806 if (NILP (type)) type = QINTEGER;
1808 else if (VECTORP (obj))
1810 /* Lisp_Vectors may represent a set of ATOMs;
1811 a set of 16 or 32 bit INTEGERs;
1812 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1814 int i;
1816 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1817 /* This vector is an ATOM set */
1819 if (NILP (type)) type = QATOM;
1820 *size_ret = XVECTOR (obj)->size;
1821 *format_ret = 32;
1822 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1823 for (i = 0; i < *size_ret; i++)
1824 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1825 (*(Atom **) data_ret) [i]
1826 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
1827 else
1828 Fsignal (Qerror, /* Qselection_error */
1829 Fcons (build_string
1830 ("all elements of selection vector must have same type"),
1831 Fcons (obj, Qnil)));
1833 #if 0 /* #### MULTIPLE doesn't work yet */
1834 else if (VECTORP (XVECTOR (obj)->contents [0]))
1835 /* This vector is an ATOM_PAIR set */
1837 if (NILP (type)) type = QATOM_PAIR;
1838 *size_ret = XVECTOR (obj)->size;
1839 *format_ret = 32;
1840 *data_ret = (unsigned char *)
1841 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1842 for (i = 0; i < *size_ret; i++)
1843 if (VECTORP (XVECTOR (obj)->contents [i]))
1845 Lisp_Object pair = XVECTOR (obj)->contents [i];
1846 if (XVECTOR (pair)->size != 2)
1847 Fsignal (Qerror,
1848 Fcons (build_string
1849 ("elements of the vector must be vectors of exactly two elements"),
1850 Fcons (pair, Qnil)));
1852 (*(Atom **) data_ret) [i * 2]
1853 = symbol_to_x_atom (dpyinfo, display,
1854 XVECTOR (pair)->contents [0]);
1855 (*(Atom **) data_ret) [(i * 2) + 1]
1856 = symbol_to_x_atom (dpyinfo, display,
1857 XVECTOR (pair)->contents [1]);
1859 else
1860 Fsignal (Qerror,
1861 Fcons (build_string
1862 ("all elements of the vector must be of the same type"),
1863 Fcons (obj, Qnil)));
1866 #endif
1867 else
1868 /* This vector is an INTEGER set, or something like it */
1870 *size_ret = XVECTOR (obj)->size;
1871 if (NILP (type)) type = QINTEGER;
1872 *format_ret = 16;
1873 for (i = 0; i < *size_ret; i++)
1874 if (CONSP (XVECTOR (obj)->contents [i]))
1875 *format_ret = 32;
1876 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1877 Fsignal (Qerror, /* Qselection_error */
1878 Fcons (build_string
1879 ("elements of selection vector must be integers or conses of integers"),
1880 Fcons (obj, Qnil)));
1882 *data_ret = (unsigned char *) xmalloc (*size_ret * (*format_ret/8));
1883 for (i = 0; i < *size_ret; i++)
1884 if (*format_ret == 32)
1885 (*((unsigned long **) data_ret)) [i]
1886 = cons_to_long (XVECTOR (obj)->contents [i]);
1887 else
1888 (*((unsigned short **) data_ret)) [i]
1889 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1892 else
1893 Fsignal (Qerror, /* Qselection_error */
1894 Fcons (build_string ("unrecognised selection data"),
1895 Fcons (obj, Qnil)));
1897 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
1900 static Lisp_Object
1901 clean_local_selection_data (obj)
1902 Lisp_Object obj;
1904 if (CONSP (obj)
1905 && INTEGERP (XCAR (obj))
1906 && CONSP (XCDR (obj))
1907 && INTEGERP (XCAR (XCDR (obj)))
1908 && NILP (XCDR (XCDR (obj))))
1909 obj = Fcons (XCAR (obj), XCDR (obj));
1911 if (CONSP (obj)
1912 && INTEGERP (XCAR (obj))
1913 && INTEGERP (XCDR (obj)))
1915 if (XINT (XCAR (obj)) == 0)
1916 return XCDR (obj);
1917 if (XINT (XCAR (obj)) == -1)
1918 return make_number (- XINT (XCDR (obj)));
1920 if (VECTORP (obj))
1922 int i;
1923 int size = XVECTOR (obj)->size;
1924 Lisp_Object copy;
1925 if (size == 1)
1926 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1927 copy = Fmake_vector (make_number (size), Qnil);
1928 for (i = 0; i < size; i++)
1929 XVECTOR (copy)->contents [i]
1930 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1931 return copy;
1933 return obj;
1936 /* Called from XTread_socket to handle SelectionNotify events.
1937 If it's the selection we are waiting for, stop waiting
1938 by setting the car of reading_selection_reply to non-nil.
1939 We store t there if the reply is successful, lambda if not. */
1941 void
1942 x_handle_selection_notify (event)
1943 XSelectionEvent *event;
1945 if (event->requestor != reading_selection_window)
1946 return;
1947 if (event->selection != reading_which_selection)
1948 return;
1950 TRACE0 ("Received SelectionNotify");
1951 XSETCAR (reading_selection_reply,
1952 (event->property != 0 ? Qt : Qlambda));
1956 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1957 Sx_own_selection_internal, 2, 2, 0,
1958 doc: /* Assert an X selection of the given TYPE with the given VALUE.
1959 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1960 \(Those are literal upper-case symbol names, since that's what X expects.)
1961 VALUE is typically a string, or a cons of two markers, but may be
1962 anything that the functions on `selection-converter-alist' know about. */)
1963 (selection_name, selection_value)
1964 Lisp_Object selection_name, selection_value;
1966 check_x ();
1967 CHECK_SYMBOL (selection_name);
1968 if (NILP (selection_value)) error ("selection-value may not be nil");
1969 x_own_selection (selection_name, selection_value);
1970 return selection_value;
1974 /* Request the selection value from the owner. If we are the owner,
1975 simply return our selection value. If we are not the owner, this
1976 will block until all of the data has arrived. */
1978 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1979 Sx_get_selection_internal, 2, 2, 0,
1980 doc: /* Return text selected from some X window.
1981 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1982 \(Those are literal upper-case symbol names, since that's what X expects.)
1983 TYPE is the type of data desired, typically `STRING'. */)
1984 (selection_symbol, target_type)
1985 Lisp_Object selection_symbol, target_type;
1987 Lisp_Object val = Qnil;
1988 struct gcpro gcpro1, gcpro2;
1989 GCPRO2 (target_type, val); /* we store newly consed data into these */
1990 check_x ();
1991 CHECK_SYMBOL (selection_symbol);
1993 #if 0 /* #### MULTIPLE doesn't work yet */
1994 if (CONSP (target_type)
1995 && XCAR (target_type) == QMULTIPLE)
1997 CHECK_VECTOR (XCDR (target_type));
1998 /* So we don't destructively modify this... */
1999 target_type = copy_multiple_data (target_type);
2001 else
2002 #endif
2003 CHECK_SYMBOL (target_type);
2005 val = x_get_local_selection (selection_symbol, target_type, 1);
2007 if (NILP (val))
2009 val = x_get_foreign_selection (selection_symbol, target_type);
2010 goto DONE;
2013 if (CONSP (val)
2014 && SYMBOLP (XCAR (val)))
2016 val = XCDR (val);
2017 if (CONSP (val) && NILP (XCDR (val)))
2018 val = XCAR (val);
2020 val = clean_local_selection_data (val);
2021 DONE:
2022 UNGCPRO;
2023 return val;
2026 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2027 Sx_disown_selection_internal, 1, 2, 0,
2028 doc: /* If we own the selection SELECTION, disown it.
2029 Disowning it means there is no such selection. */)
2030 (selection, time)
2031 Lisp_Object selection;
2032 Lisp_Object time;
2034 Time timestamp;
2035 Atom selection_atom;
2036 struct selection_input_event event;
2037 Display *display;
2038 struct x_display_info *dpyinfo;
2039 struct frame *sf = SELECTED_FRAME ();
2041 check_x ();
2042 display = FRAME_X_DISPLAY (sf);
2043 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2044 CHECK_SYMBOL (selection);
2045 if (NILP (time))
2046 timestamp = last_event_timestamp;
2047 else
2048 timestamp = cons_to_long (time);
2050 if (NILP (assq_no_quit (selection, Vselection_alist)))
2051 return Qnil; /* Don't disown the selection when we're not the owner. */
2053 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
2055 BLOCK_INPUT;
2056 XSetSelectionOwner (display, selection_atom, None, timestamp);
2057 UNBLOCK_INPUT;
2059 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2060 generated for a window which owns the selection when that window sets
2061 the selection owner to None. The NCD server does, the MIT Sun4 server
2062 doesn't. So we synthesize one; this means we might get two, but
2063 that's ok, because the second one won't have any effect. */
2064 SELECTION_EVENT_DISPLAY (&event) = display;
2065 SELECTION_EVENT_SELECTION (&event) = selection_atom;
2066 SELECTION_EVENT_TIME (&event) = timestamp;
2067 x_handle_selection_clear ((struct input_event *) &event);
2069 return Qt;
2072 /* Get rid of all the selections in buffer BUFFER.
2073 This is used when we kill a buffer. */
2075 void
2076 x_disown_buffer_selections (buffer)
2077 Lisp_Object buffer;
2079 Lisp_Object tail;
2080 struct buffer *buf = XBUFFER (buffer);
2082 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
2084 Lisp_Object elt, value;
2085 elt = XCAR (tail);
2086 value = XCDR (elt);
2087 if (CONSP (value) && MARKERP (XCAR (value))
2088 && XMARKER (XCAR (value))->buffer == buf)
2089 Fx_disown_selection_internal (XCAR (elt), Qnil);
2093 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2094 0, 1, 0,
2095 doc: /* Whether the current Emacs process owns the given X Selection.
2096 The arg should be the name of the selection in question, typically one of
2097 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2098 \(Those are literal upper-case symbol names, since that's what X expects.)
2099 For convenience, the symbol nil is the same as `PRIMARY',
2100 and t is the same as `SECONDARY'. */)
2101 (selection)
2102 Lisp_Object selection;
2104 check_x ();
2105 CHECK_SYMBOL (selection);
2106 if (EQ (selection, Qnil)) selection = QPRIMARY;
2107 if (EQ (selection, Qt)) selection = QSECONDARY;
2109 if (NILP (Fassq (selection, Vselection_alist)))
2110 return Qnil;
2111 return Qt;
2114 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2115 0, 1, 0,
2116 doc: /* Whether there is an owner for the given X Selection.
2117 The arg should be the name of the selection in question, typically one of
2118 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2119 \(Those are literal upper-case symbol names, since that's what X expects.)
2120 For convenience, the symbol nil is the same as `PRIMARY',
2121 and t is the same as `SECONDARY'. */)
2122 (selection)
2123 Lisp_Object selection;
2125 Window owner;
2126 Atom atom;
2127 Display *dpy;
2128 struct frame *sf = SELECTED_FRAME ();
2130 /* It should be safe to call this before we have an X frame. */
2131 if (! FRAME_X_P (sf))
2132 return Qnil;
2134 dpy = FRAME_X_DISPLAY (sf);
2135 CHECK_SYMBOL (selection);
2136 if (!NILP (Fx_selection_owner_p (selection)))
2137 return Qt;
2138 if (EQ (selection, Qnil)) selection = QPRIMARY;
2139 if (EQ (selection, Qt)) selection = QSECONDARY;
2140 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
2141 if (atom == 0)
2142 return Qnil;
2143 BLOCK_INPUT;
2144 owner = XGetSelectionOwner (dpy, atom);
2145 UNBLOCK_INPUT;
2146 return (owner ? Qt : Qnil);
2150 #ifdef CUT_BUFFER_SUPPORT
2152 /* Ensure that all 8 cut buffers exist. ICCCM says we gotta... */
2153 static void
2154 initialize_cut_buffers (display, window)
2155 Display *display;
2156 Window window;
2158 unsigned char *data = (unsigned char *) "";
2159 BLOCK_INPUT;
2160 #define FROB(atom) XChangeProperty (display, window, atom, XA_STRING, 8, \
2161 PropModeAppend, data, 0)
2162 FROB (XA_CUT_BUFFER0);
2163 FROB (XA_CUT_BUFFER1);
2164 FROB (XA_CUT_BUFFER2);
2165 FROB (XA_CUT_BUFFER3);
2166 FROB (XA_CUT_BUFFER4);
2167 FROB (XA_CUT_BUFFER5);
2168 FROB (XA_CUT_BUFFER6);
2169 FROB (XA_CUT_BUFFER7);
2170 #undef FROB
2171 UNBLOCK_INPUT;
2175 #define CHECK_CUT_BUFFER(symbol) \
2176 { CHECK_SYMBOL ((symbol)); \
2177 if (!EQ((symbol), QCUT_BUFFER0) && !EQ((symbol), QCUT_BUFFER1) \
2178 && !EQ((symbol), QCUT_BUFFER2) && !EQ((symbol), QCUT_BUFFER3) \
2179 && !EQ((symbol), QCUT_BUFFER4) && !EQ((symbol), QCUT_BUFFER5) \
2180 && !EQ((symbol), QCUT_BUFFER6) && !EQ((symbol), QCUT_BUFFER7)) \
2181 Fsignal (Qerror, \
2182 Fcons (build_string ("doesn't name a cut buffer"), \
2183 Fcons ((symbol), Qnil))); \
2186 DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
2187 Sx_get_cut_buffer_internal, 1, 1, 0,
2188 doc: /* Returns the value of the named cut buffer (typically CUT_BUFFER0). */)
2189 (buffer)
2190 Lisp_Object buffer;
2192 Window window;
2193 Atom buffer_atom;
2194 unsigned char *data;
2195 int bytes;
2196 Atom type;
2197 int format;
2198 unsigned long size;
2199 Lisp_Object ret;
2200 Display *display;
2201 struct x_display_info *dpyinfo;
2202 struct frame *sf = SELECTED_FRAME ();
2204 check_x ();
2205 display = FRAME_X_DISPLAY (sf);
2206 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2207 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2208 CHECK_CUT_BUFFER (buffer);
2209 buffer_atom = symbol_to_x_atom (dpyinfo, display, buffer);
2211 x_get_window_property (display, window, buffer_atom, &data, &bytes,
2212 &type, &format, &size, 0);
2213 if (!data || !format)
2214 return Qnil;
2216 if (format != 8 || type != XA_STRING)
2217 Fsignal (Qerror,
2218 Fcons (build_string ("cut buffer doesn't contain 8-bit data"),
2219 Fcons (x_atom_to_symbol (display, type),
2220 Fcons (make_number (format), Qnil))));
2222 ret = (bytes ? make_string ((char *) data, bytes) : Qnil);
2223 /* Use xfree, not XFree, because x_get_window_property
2224 calls xmalloc itself. */
2225 xfree (data);
2226 return ret;
2230 DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
2231 Sx_store_cut_buffer_internal, 2, 2, 0,
2232 doc: /* Sets the value of the named cut buffer (typically CUT_BUFFER0). */)
2233 (buffer, string)
2234 Lisp_Object buffer, string;
2236 Window window;
2237 Atom buffer_atom;
2238 unsigned char *data;
2239 int bytes;
2240 int bytes_remaining;
2241 int max_bytes;
2242 Display *display;
2243 struct frame *sf = SELECTED_FRAME ();
2245 check_x ();
2246 display = FRAME_X_DISPLAY (sf);
2247 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2249 max_bytes = SELECTION_QUANTUM (display);
2250 if (max_bytes > MAX_SELECTION_QUANTUM)
2251 max_bytes = MAX_SELECTION_QUANTUM;
2253 CHECK_CUT_BUFFER (buffer);
2254 CHECK_STRING (string);
2255 buffer_atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf),
2256 display, buffer);
2257 data = (unsigned char *) SDATA (string);
2258 bytes = SBYTES (string);
2259 bytes_remaining = bytes;
2261 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
2263 initialize_cut_buffers (display, window);
2264 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
2267 BLOCK_INPUT;
2269 /* Don't mess up with an empty value. */
2270 if (!bytes_remaining)
2271 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2272 PropModeReplace, data, 0);
2274 while (bytes_remaining)
2276 int chunk = (bytes_remaining < max_bytes
2277 ? bytes_remaining : max_bytes);
2278 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2279 (bytes_remaining == bytes
2280 ? PropModeReplace
2281 : PropModeAppend),
2282 data, chunk);
2283 data += chunk;
2284 bytes_remaining -= chunk;
2286 UNBLOCK_INPUT;
2287 return string;
2291 DEFUN ("x-rotate-cut-buffers-internal", Fx_rotate_cut_buffers_internal,
2292 Sx_rotate_cut_buffers_internal, 1, 1, 0,
2293 doc: /* Rotate the values of the cut buffers by the given number of step.
2294 Positive means shift the values forward, negative means backward. */)
2296 Lisp_Object n;
2298 Window window;
2299 Atom props[8];
2300 Display *display;
2301 struct frame *sf = SELECTED_FRAME ();
2303 check_x ();
2304 display = FRAME_X_DISPLAY (sf);
2305 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2306 CHECK_NUMBER (n);
2307 if (XINT (n) == 0)
2308 return n;
2309 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
2311 initialize_cut_buffers (display, window);
2312 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
2315 props[0] = XA_CUT_BUFFER0;
2316 props[1] = XA_CUT_BUFFER1;
2317 props[2] = XA_CUT_BUFFER2;
2318 props[3] = XA_CUT_BUFFER3;
2319 props[4] = XA_CUT_BUFFER4;
2320 props[5] = XA_CUT_BUFFER5;
2321 props[6] = XA_CUT_BUFFER6;
2322 props[7] = XA_CUT_BUFFER7;
2323 BLOCK_INPUT;
2324 XRotateWindowProperties (display, window, props, 8, XINT (n));
2325 UNBLOCK_INPUT;
2326 return n;
2329 #endif
2331 void
2332 syms_of_xselect ()
2334 defsubr (&Sx_get_selection_internal);
2335 defsubr (&Sx_own_selection_internal);
2336 defsubr (&Sx_disown_selection_internal);
2337 defsubr (&Sx_selection_owner_p);
2338 defsubr (&Sx_selection_exists_p);
2340 #ifdef CUT_BUFFER_SUPPORT
2341 defsubr (&Sx_get_cut_buffer_internal);
2342 defsubr (&Sx_store_cut_buffer_internal);
2343 defsubr (&Sx_rotate_cut_buffers_internal);
2344 #endif
2346 reading_selection_reply = Fcons (Qnil, Qnil);
2347 staticpro (&reading_selection_reply);
2348 reading_selection_window = 0;
2349 reading_which_selection = 0;
2351 property_change_wait_list = 0;
2352 prop_location_identifier = 0;
2353 property_change_reply = Fcons (Qnil, Qnil);
2354 staticpro (&property_change_reply);
2356 Vselection_alist = Qnil;
2357 staticpro (&Vselection_alist);
2359 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
2360 doc: /* An alist associating X Windows selection-types with functions.
2361 These functions are called to convert the selection, with three args:
2362 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2363 a desired type to which the selection should be converted;
2364 and the local selection value (whatever was given to `x-own-selection').
2366 The function should return the value to send to the X server
2367 \(typically a string). A return value of nil
2368 means that the conversion could not be done.
2369 A return value which is the symbol `NULL'
2370 means that a side-effect was executed,
2371 and there is no meaningful selection value. */);
2372 Vselection_converter_alist = Qnil;
2374 DEFVAR_LISP ("x-lost-selection-hooks", &Vx_lost_selection_hooks,
2375 doc: /* A list of functions to be called when Emacs loses an X selection.
2376 \(This happens when some other X client makes its own selection
2377 or when a Lisp program explicitly clears the selection.)
2378 The functions are called with one argument, the selection type
2379 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2380 Vx_lost_selection_hooks = Qnil;
2382 DEFVAR_LISP ("x-sent-selection-hooks", &Vx_sent_selection_hooks,
2383 doc: /* A list of functions to be called when Emacs answers a selection request.
2384 The functions are called with four arguments:
2385 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2386 - the selection-type which Emacs was asked to convert the
2387 selection into before sending (for example, `STRING' or `LENGTH');
2388 - a flag indicating success or failure for responding to the request.
2389 We might have failed (and declined the request) for any number of reasons,
2390 including being asked for a selection that we no longer own, or being asked
2391 to convert into a type that we don't know about or that is inappropriate.
2392 This hook doesn't let you change the behavior of Emacs's selection replies,
2393 it merely informs you that they have happened. */);
2394 Vx_sent_selection_hooks = Qnil;
2396 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
2397 doc: /* Coding system for communicating with other X clients.
2398 When sending or receiving text via cut_buffer, selection, and clipboard,
2399 the text is encoded or decoded by this coding system.
2400 The default value is `compound-text-with-extensions'. */);
2401 Vselection_coding_system = intern ("compound-text-with-extensions");
2403 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
2404 doc: /* Coding system for the next communication with other X clients.
2405 Usually, `selection-coding-system' is used for communicating with
2406 other X clients. But, if this variable is set, it is used for the
2407 next communication only. After the communication, this variable is
2408 set to nil. */);
2409 Vnext_selection_coding_system = Qnil;
2411 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
2412 doc: /* Number of milliseconds to wait for a selection reply.
2413 If the selection owner doesn't reply in this time, we give up.
2414 A value of 0 means wait as long as necessary. This is initialized from the
2415 \"*selectionTimeout\" resource. */);
2416 x_selection_timeout = 0;
2418 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
2419 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
2420 QSTRING = intern ("STRING"); staticpro (&QSTRING);
2421 QINTEGER = intern ("INTEGER"); staticpro (&QINTEGER);
2422 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
2423 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2424 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
2425 QCOMPOUND_TEXT = intern ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
2426 QUTF8_STRING = intern ("UTF8_STRING"); staticpro (&QUTF8_STRING);
2427 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2428 QDELETE = intern ("DELETE"); staticpro (&QDELETE);
2429 QMULTIPLE = intern ("MULTIPLE"); staticpro (&QMULTIPLE);
2430 QINCR = intern ("INCR"); staticpro (&QINCR);
2431 QEMACS_TMP = intern ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
2432 QTARGETS = intern ("TARGETS"); staticpro (&QTARGETS);
2433 QATOM = intern ("ATOM"); staticpro (&QATOM);
2434 QATOM_PAIR = intern ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
2435 QNULL = intern ("NULL"); staticpro (&QNULL);
2436 Qcompound_text_with_extensions = intern ("compound-text-with-extensions");
2437 staticpro (&Qcompound_text_with_extensions);
2439 #ifdef CUT_BUFFER_SUPPORT
2440 QCUT_BUFFER0 = intern ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
2441 QCUT_BUFFER1 = intern ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
2442 QCUT_BUFFER2 = intern ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
2443 QCUT_BUFFER3 = intern ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
2444 QCUT_BUFFER4 = intern ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
2445 QCUT_BUFFER5 = intern ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
2446 QCUT_BUFFER6 = intern ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
2447 QCUT_BUFFER7 = intern ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
2448 #endif