Replace bcopy, bzero, bcmp by memcpy, memmove, memset, memcmp
[bpt/emacs.git] / src / xselect.c
index c5e4170..df260e3 100644 (file)
@@ -1,13 +1,13 @@
 /* X Selection processing for Emacs.
    Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003,
-                 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+                 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,15 +15,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 /* Rewritten by jwz */
 
 #include <config.h>
 #include <stdio.h>      /* termhooks.h needs this */
+#include <setjmp.h>
 
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -46,44 +45,44 @@ Boston, MA 02110-1301, USA.  */
 
 struct prop_location;
 
-static Lisp_Object x_atom_to_symbol P_ ((Display *dpy, Atom atom));
-static Atom symbol_to_x_atom P_ ((struct x_display_info *, Display *,
-                                 Lisp_Object));
-static void x_own_selection P_ ((Lisp_Object, Lisp_Object));
-static Lisp_Object x_get_local_selection P_ ((Lisp_Object, Lisp_Object, int));
-static void x_decline_selection_request P_ ((struct input_event *));
-static Lisp_Object x_selection_request_lisp_error P_ ((Lisp_Object));
-static Lisp_Object queue_selection_requests_unwind P_ ((Lisp_Object));
-static Lisp_Object some_frame_on_display P_ ((struct x_display_info *));
-static Lisp_Object x_catch_errors_unwind P_ ((Lisp_Object));
-static void x_reply_selection_request P_ ((struct input_event *, int,
-                                          unsigned char *, int, Atom));
-static int waiting_for_other_props_on_window P_ ((Display *, Window));
-static struct prop_location *expect_property_change P_ ((Display *, Window,
-                                                        Atom, int));
-static void unexpect_property_change P_ ((struct prop_location *));
-static Lisp_Object wait_for_property_change_unwind P_ ((Lisp_Object));
-static void wait_for_property_change P_ ((struct prop_location *));
-static Lisp_Object x_get_foreign_selection P_ ((Lisp_Object,
-                                                Lisp_Object,
-                                                Lisp_Object));
-static void x_get_window_property P_ ((Display *, Window, Atom,
-                                      unsigned char **, int *,
-                                      Atom *, int *, unsigned long *, int));
-static void receive_incremental_selection P_ ((Display *, Window, Atom,
-                                              Lisp_Object, unsigned,
-                                              unsigned char **, int *,
-                                              Atom *, int *, unsigned long *));
-static Lisp_Object x_get_window_property_as_lisp_data P_ ((Display *,
-                                                          Window, Atom,
-                                                          Lisp_Object, Atom));
-static Lisp_Object selection_data_to_lisp_data P_ ((Display *, unsigned char *,
-                                                   int, Atom, int));
-static void lisp_data_to_selection_data P_ ((Display *, Lisp_Object,
-                                            unsigned char **, Atom *,
-                                            unsigned *, int *, int *));
-static Lisp_Object clean_local_selection_data P_ ((Lisp_Object));
-static void initialize_cut_buffers P_ ((Display *, Window));
+static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
+static Atom symbol_to_x_atom (struct x_display_info *, Display *,
+                              Lisp_Object);
+static void x_own_selection (Lisp_Object, Lisp_Object);
+static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int);
+static void x_decline_selection_request (struct input_event *);
+static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
+static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
+static Lisp_Object some_frame_on_display (struct x_display_info *);
+static Lisp_Object x_catch_errors_unwind (Lisp_Object);
+static void x_reply_selection_request (struct input_event *, int,
+                                       unsigned char *, int, Atom);
+static int waiting_for_other_props_on_window (Display *, Window);
+static struct prop_location *expect_property_change (Display *, Window,
+                                                     Atom, int);
+static void unexpect_property_change (struct prop_location *);
+static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
+static void wait_for_property_change (struct prop_location *);
+static Lisp_Object x_get_foreign_selection (Lisp_Object,
+                                            Lisp_Object,
+                                            Lisp_Object);
+static void x_get_window_property (Display *, Window, Atom,
+                                   unsigned char **, int *,
+                                   Atom *, int *, unsigned long *, int);
+static void receive_incremental_selection (Display *, Window, Atom,
+                                           Lisp_Object, unsigned,
+                                           unsigned char **, int *,
+                                           Atom *, int *, unsigned long *);
+static Lisp_Object x_get_window_property_as_lisp_data (Display *,
+                                                       Window, Atom,
+                                                       Lisp_Object, Atom);
+static Lisp_Object selection_data_to_lisp_data (Display *, unsigned char *,
+                                                int, Atom, int);
+static void lisp_data_to_selection_data (Display *, Lisp_Object,
+                                         unsigned char **, Atom *,
+                                         unsigned *, int *, int *);
+static Lisp_Object clean_local_selection_data (Lisp_Object);
+static void initialize_cut_buffers (Display *, Window);
 
 
 /* Printing traces to stderr.  */
@@ -123,13 +122,6 @@ Lisp_Object QCUT_BUFFER0, QCUT_BUFFER1, QCUT_BUFFER2, QCUT_BUFFER3,
 
 static Lisp_Object Vx_lost_selection_functions;
 static Lisp_Object Vx_sent_selection_functions;
-/* Coding system for communicating with other X clients via selection
-   and clipboard.  */
-static Lisp_Object Vselection_coding_system;
-
-/* Coding system for the next communicating with other X clients.  */
-static Lisp_Object Vnext_selection_coding_system;
-
 static Lisp_Object Qforeign_selection;
 
 /* If this is a smaller number than the max-request-size of the display,
@@ -140,11 +132,7 @@ static Lisp_Object Qforeign_selection;
    incremental transfer stuff, but it might improve server performance.  */
 #define MAX_SELECTION_QUANTUM 0xFFFFFF
 
-#ifdef HAVE_X11R4
 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
-#else
-#define SELECTION_QUANTUM(dpy) (((dpy)->max_request_size << 2) - 100)
-#endif
 
 /* The timestamp of the last input event Emacs received from the X server.  */
 /* Defined in keyboard.c.  */
@@ -195,8 +183,7 @@ static int x_queue_selection_requests;
 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later.  */
 
 static void
-x_queue_event (event)
-     struct input_event *event;
+x_queue_event (struct input_event *event)
 {
   struct selection_event_queue *queue_tmp;
 
@@ -204,7 +191,7 @@ x_queue_event (event)
      This only happens for large requests which uses the incremental protocol.  */
   for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
     {
-      if (!bcmp (&queue_tmp->event, event, sizeof (*event)))
+      if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
        {
          TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp);
          x_decline_selection_request (event);
@@ -227,7 +214,7 @@ x_queue_event (event)
 /* Start queuing SELECTION_REQUEST_EVENT events.  */
 
 static void
-x_start_queuing_selection_requests ()
+x_start_queuing_selection_requests (void)
 {
   if (x_queue_selection_requests)
     abort ();
@@ -239,7 +226,7 @@ x_start_queuing_selection_requests ()
 /* Stop queuing SELECTION_REQUEST_EVENT events.  */
 
 static void
-x_stop_queuing_selection_requests ()
+x_stop_queuing_selection_requests (void)
 {
   TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
   --x_queue_selection_requests;
@@ -262,10 +249,7 @@ x_stop_queuing_selection_requests ()
    roundtrip whenever possible.  */
 
 static Atom
-symbol_to_x_atom (dpyinfo, display, sym)
-     struct x_display_info *dpyinfo;
-     Display *display;
-     Lisp_Object sym;
+symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym)
 {
   Atom val;
   if (NILP (sym))          return 0;
@@ -309,9 +293,7 @@ symbol_to_x_atom (dpyinfo, display, sym)
    and calls to intern whenever possible.  */
 
 static Lisp_Object
-x_atom_to_symbol (dpy, atom)
-     Display *dpy;
-     Atom atom;
+x_atom_to_symbol (Display *dpy, Atom atom)
 {
   struct x_display_info *dpyinfo;
   char *str;
@@ -394,16 +376,22 @@ x_atom_to_symbol (dpy, atom)
    our selection.  */
 
 static void
-x_own_selection (selection_name, selection_value)
-     Lisp_Object selection_name, selection_value;
+x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
 {
   struct frame *sf = SELECTED_FRAME ();
-  Window selecting_window = FRAME_X_WINDOW (sf);
-  Display *display = FRAME_X_DISPLAY (sf);
+  Window selecting_window;
+  Display *display;
   Time time = last_event_timestamp;
   Atom selection_atom;
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
+  struct x_display_info *dpyinfo;
 
+  if (! FRAME_X_P (sf))
+    return;
+
+  selecting_window = FRAME_X_WINDOW (sf);
+  display = FRAME_X_DISPLAY (sf);
+  dpyinfo = FRAME_X_DISPLAY_INFO (sf);
+  
   CHECK_SYMBOL (selection_name);
   selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
 
@@ -435,7 +423,7 @@ x_own_selection (selection_name, selection_value)
     if (!NILP (prev_value))
       {
        Lisp_Object rest;       /* we know it's not the CAR, so it's easy.  */
-       for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
+       for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
          if (EQ (prev_value, Fcar (XCDR (rest))))
            {
              XSETCDR (rest, Fcdr (XCDR (rest)));
@@ -454,9 +442,7 @@ x_own_selection (selection_name, selection_value)
    This calls random Lisp code, and may signal or gc.  */
 
 static Lisp_Object
-x_get_local_selection (selection_symbol, target_type, local_request)
-     Lisp_Object selection_symbol, target_type;
-     int local_request;
+x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request)
 {
   Lisp_Object local_value;
   Lisp_Object handler_fn, value, type, check;
@@ -566,8 +552,7 @@ x_get_local_selection (selection_symbol, target_type, local_request)
    meaning we were unable to do what they wanted.  */
 
 static void
-x_decline_selection_request (event)
-     struct input_event *event;
+x_decline_selection_request (struct input_event *event)
 {
   XSelectionEvent reply;
 
@@ -602,8 +587,7 @@ static struct x_display_info *selection_request_dpyinfo;
    before we throw to top-level or go into the debugger or whatever.  */
 
 static Lisp_Object
-x_selection_request_lisp_error (ignore)
-     Lisp_Object ignore;
+x_selection_request_lisp_error (Lisp_Object ignore)
 {
   if (x_selection_current_request != 0
       && selection_request_dpyinfo->display)
@@ -612,8 +596,7 @@ x_selection_request_lisp_error (ignore)
 }
 
 static Lisp_Object
-x_catch_errors_unwind (dummy)
-     Lisp_Object dummy;
+x_catch_errors_unwind (Lisp_Object dummy)
 {
   BLOCK_INPUT;
   x_uncatch_errors ();
@@ -639,10 +622,10 @@ struct prop_location
   struct prop_location *next;
 };
 
-static struct prop_location *expect_property_change ();
-static void wait_for_property_change ();
-static void unexpect_property_change ();
-static int waiting_for_other_props_on_window ();
+static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
+static void wait_for_property_change (struct prop_location *location);
+static void unexpect_property_change (struct prop_location *location);
+static int waiting_for_other_props_on_window (Display *display, Window window);
 
 static int prop_location_identifier;
 
@@ -653,8 +636,7 @@ static struct prop_location *property_change_reply_object;
 static struct prop_location *property_change_wait_list;
 
 static Lisp_Object
-queue_selection_requests_unwind (tem)
-     Lisp_Object tem;
+queue_selection_requests_unwind (Lisp_Object tem)
 {
   x_stop_queuing_selection_requests ();
   return Qnil;
@@ -664,14 +646,14 @@ queue_selection_requests_unwind (tem)
    Return nil if there is none.  */
 
 static Lisp_Object
-some_frame_on_display (dpyinfo)
-     struct x_display_info *dpyinfo;
+some_frame_on_display (struct x_display_info *dpyinfo)
 {
   Lisp_Object list, frame;
 
   FOR_EACH_FRAME (list, frame)
     {
-      if (FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
+      if (FRAME_X_P (XFRAME (frame))
+          && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
        return frame;
     }
 
@@ -688,11 +670,7 @@ static int x_reply_selection_request_cnt;
 #endif  /* TRACE_SELECTION */
 
 static void
-x_reply_selection_request (event, format, data, size, type)
-     struct input_event *event;
-     int format, size;
-     unsigned char *data;
-     Atom type;
+x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type)
 {
   XSelectionEvent reply;
   Display *display = SELECTION_EVENT_DISPLAY (event);
@@ -813,7 +791,7 @@ x_reply_selection_request (event, format, data, size, type)
        {
           int i = ((bytes_remaining < max_bytes)
                    ? bytes_remaining
-                   : max_bytes);
+                   : max_bytes) / format_bytes;
 
          BLOCK_INPUT;
 
@@ -821,15 +799,18 @@ x_reply_selection_request (event, format, data, size, type)
            = expect_property_change (display, window, reply.property,
                                      PropertyDelete);
 
-         TRACE1 ("Sending increment of %d bytes", i);
+         TRACE1 ("Sending increment of %d elements", i);
          TRACE1 ("Set %s to increment data",
                  XGetAtomName (display, reply.property));
 
          /* Append the next chunk of data to the property.  */
          XChangeProperty (display, window, reply.property, type, format,
-                          PropModeAppend, data, i / format_bytes);
-         bytes_remaining -= i;
-         data += i;
+                          PropModeAppend, data, i);
+         bytes_remaining -= i * format_bytes;
+         if (format == 32)
+           data += i * sizeof (long);
+         else
+           data += i * format_bytes;
          XFlush (display);
          had_errors = x_had_errors_p (display);
          UNBLOCK_INPUT;
@@ -882,8 +863,7 @@ x_reply_selection_request (event, format, data, size, type)
    This is called from keyboard.c when such an event is found in the queue.  */
 
 static void
-x_handle_selection_request (event)
-     struct input_event *event;
+x_handle_selection_request (struct input_event *event)
 {
   struct gcpro gcpro1, gcpro2, gcpro3;
   Lisp_Object local_selection_data;
@@ -1003,8 +983,7 @@ x_handle_selection_request (event)
    This is called from keyboard.c when such an event is found in the queue.  */
 
 static void
-x_handle_selection_clear (event)
-     struct input_event *event;
+x_handle_selection_clear (struct input_event *event)
 {
   Display *display = SELECTION_EVENT_DISPLAY (event);
   Atom selection = SELECTION_EVENT_SELECTION (event);
@@ -1024,7 +1003,7 @@ x_handle_selection_clear (event)
      to see if this Emacs job now owns the selection
      through that display.  */
   for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
-    if (t_dpyinfo->kboard == dpyinfo->kboard)
+    if (t_dpyinfo->terminal->kboard == dpyinfo->terminal->kboard)
       {
        Window owner_window
          = XGetSelectionOwner (t_dpyinfo->display, selection);
@@ -1035,7 +1014,7 @@ x_handle_selection_clear (event)
          }
       }
   UNBLOCK_INPUT;
-
+  
   selection_symbol = x_atom_to_symbol (display, selection);
 
   local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
@@ -1062,7 +1041,7 @@ x_handle_selection_clear (event)
   else
     {
       Lisp_Object rest;
-      for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
+      for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
        if (EQ (local_selection_data, Fcar (XCDR (rest))))
          {
            XSETCDR (rest, Fcdr (XCDR (rest)));
@@ -1086,8 +1065,7 @@ x_handle_selection_clear (event)
 }
 
 void
-x_handle_selection_event (event)
-     struct input_event *event;
+x_handle_selection_event (struct input_event *event)
 {
   TRACE0 ("x_handle_selection_event");
 
@@ -1107,8 +1085,7 @@ x_handle_selection_event (event)
    We do this when about to delete a frame.  */
 
 void
-x_clear_frame_selections (f)
-     FRAME_PTR f;
+x_clear_frame_selections (FRAME_PTR f)
 {
   Lisp_Object frame;
   Lisp_Object rest;
@@ -1143,7 +1120,7 @@ x_clear_frame_selections (f)
     }
 
   /* Delete elements after the beginning of Vselection_alist.  */
-  for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
+  for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
     if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
       {
        /* Let random Lisp code notice that the selection has been stolen.  */
@@ -1169,9 +1146,7 @@ x_clear_frame_selections (f)
    are on the list of what we are waiting for.  */
 
 static int
-waiting_for_other_props_on_window (display, window)
-     Display *display;
-     Window window;
+waiting_for_other_props_on_window (Display *display, Window window)
 {
   struct prop_location *rest = property_change_wait_list;
   while (rest)
@@ -1188,11 +1163,7 @@ waiting_for_other_props_on_window (display, window)
    this awaited property change.  */
 
 static struct prop_location *
-expect_property_change (display, window, property, state)
-     Display *display;
-     Window window;
-     Atom property;
-     int state;
+expect_property_change (Display *display, Window window, Atom property, int state)
 {
   struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
   pl->identifier = ++prop_location_identifier;
@@ -1210,8 +1181,7 @@ expect_property_change (display, window, property, state)
    IDENTIFIER is the number that uniquely identifies the entry.  */
 
 static void
-unexpect_property_change (location)
-     struct prop_location *location;
+unexpect_property_change (struct prop_location *location)
 {
   struct prop_location *prev = 0, *rest = property_change_wait_list;
   while (rest)
@@ -1233,8 +1203,7 @@ unexpect_property_change (location)
 /* Remove the property change expectation element for IDENTIFIER.  */
 
 static Lisp_Object
-wait_for_property_change_unwind (loc)
-     Lisp_Object loc;
+wait_for_property_change_unwind (Lisp_Object loc)
 {
   struct prop_location *location = XSAVE_VALUE (loc)->pointer;
 
@@ -1248,8 +1217,7 @@ wait_for_property_change_unwind (loc)
    IDENTIFIER should be the value that expect_property_change returned.  */
 
 static void
-wait_for_property_change (location)
-     struct prop_location *location;
+wait_for_property_change (struct prop_location *location)
 {
   int secs, usecs;
   int count = SPECPDL_INDEX ();
@@ -1287,8 +1255,7 @@ wait_for_property_change (location)
 /* Called from XTread_socket in response to a PropertyNotify event.  */
 
 void
-x_handle_property_notify (event)
-     XPropertyEvent *event;
+x_handle_property_notify (XPropertyEvent *event)
 {
   struct prop_location *prev = 0, *rest = property_change_wait_list;
 
@@ -1379,21 +1346,29 @@ static Window reading_selection_window;
    Converts this to Lisp data and returns it.  */
 
 static Lisp_Object
-x_get_foreign_selection (selection_symbol, target_type, time_stamp)
-     Lisp_Object selection_symbol, target_type, time_stamp;
+x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
 {
   struct frame *sf = SELECTED_FRAME ();
-  Window requestor_window = FRAME_X_WINDOW (sf);
-  Display *display = FRAME_X_DISPLAY (sf);
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (sf);
+  Window requestor_window;
+  Display *display;
+  struct x_display_info *dpyinfo;
   Time requestor_time = last_event_timestamp;
-  Atom target_property = dpyinfo->Xatom_EMACS_TMP;
-  Atom selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
+  Atom target_property;
+  Atom selection_atom;
   Atom type_atom;
   int secs, usecs;
   int count = SPECPDL_INDEX ();
   Lisp_Object frame;
 
+  if (! FRAME_X_P (sf))
+    return Qnil;
+
+  requestor_window = FRAME_X_WINDOW (sf);
+  display = FRAME_X_DISPLAY (sf);
+  dpyinfo = FRAME_X_DISPLAY_INFO (sf);
+  target_property = dpyinfo->Xatom_EMACS_TMP;
+  selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
+
   if (CONSP (target_type))
     type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
   else
@@ -1464,7 +1439,7 @@ x_get_foreign_selection (selection_symbol, target_type, time_stamp)
   if (NILP (XCAR (reading_selection_reply)))
     error ("Timed out waiting for reply from selection owner");
   if (EQ (XCAR (reading_selection_reply), Qlambda))
-    error ("No `%s' selection", SDATA (SYMBOL_NAME (selection_symbol)));
+    return Qnil;
 
   /* Otherwise, the selection is waiting for us on the requested property.  */
   return
@@ -1559,7 +1534,7 @@ x_get_window_property (display, window, property, data_ret, bytes_ret,
           elements."
          This applies even if long is more than 32 bits, the X library
          converts from 32 bit elements received from the X server to long
-         and passes the long array to us.  Thus, for that case bcopy can not
+         and passes the long array to us.  Thus, for that case memcpy can not
          be used.  We convert to a 32 bit type here, because so much code
          assume on that.
 
@@ -1581,7 +1556,7 @@ x_get_window_property (display, window, property, data_ret, bytes_ret,
       else
         {
           *actual_size_ret *= *actual_format_ret / 8;
-          bcopy (tmp_data, (*data_ret) + offset, *actual_size_ret);
+          memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
           offset += *actual_size_ret;
         }
 
@@ -1664,7 +1639,7 @@ receive_incremental_selection (display, window, property, target_type,
            XSelectInput (display, window, STANDARD_EVENT_SET);
          /* Use xfree, not XFree, because x_get_window_property
             calls xmalloc itself.  */
-         if (tmp_data) xfree (tmp_data);
+         xfree (tmp_data);
          break;
        }
 
@@ -1683,7 +1658,7 @@ receive_incremental_selection (display, window, property, target_type,
          *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
        }
 
-      bcopy (tmp_data, (*data_ret) + offset, tmp_size_bytes);
+      memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
       offset += tmp_size_bytes;
 
       /* Use xfree, not XFree, because x_get_window_property
@@ -1801,11 +1776,7 @@ x_get_window_property_as_lisp_data (display, window, property, target_type,
 
 
 static Lisp_Object
-selection_data_to_lisp_data (display, data, size, type, format)
-     Display *display;
-     unsigned char *data;
-     Atom type;
-     int size, format;
+selection_data_to_lisp_data (Display *display, unsigned char *data, int size, Atom type, int format)
 {
   struct x_display_info *dpyinfo = x_display_info_for_display (display);
 
@@ -2063,8 +2034,7 @@ lisp_data_to_selection_data (display, obj,
 }
 
 static Lisp_Object
-clean_local_selection_data (obj)
-     Lisp_Object obj;
+clean_local_selection_data (Lisp_Object obj)
 {
   if (CONSP (obj)
       && INTEGERP (XCAR (obj))
@@ -2104,8 +2074,7 @@ clean_local_selection_data (obj)
    We store t there if the reply is successful, lambda if not.  */
 
 void
-x_handle_selection_notify (event)
-     XSelectionEvent *event;
+x_handle_selection_notify (XSelectionEvent *event)
 {
   if (event->requestor != reading_selection_window)
     return;
@@ -2209,6 +2178,9 @@ Disowning it means there is no such selection.  */)
   struct frame *sf = SELECTED_FRAME ();
 
   check_x ();
+  if (! FRAME_X_P (sf))
+    return Qnil;
+
   display = FRAME_X_DISPLAY (sf);
   dpyinfo = FRAME_X_DISPLAY_INFO (sf);
   CHECK_SYMBOL (selection);
@@ -2243,8 +2215,7 @@ Disowning it means there is no such selection.  */)
    This is used when we kill a buffer.  */
 
 void
-x_disown_buffer_selections (buffer)
-     Lisp_Object buffer;
+x_disown_buffer_selections (Lisp_Object buffer)
 {
   Lisp_Object tail;
   struct buffer *buf = XBUFFER (buffer);
@@ -2321,9 +2292,7 @@ and t is the same as `SECONDARY'.  */)
 
 /* Ensure that all 8 cut buffers exist.  ICCCM says we gotta...  */
 static void
-initialize_cut_buffers (display, window)
-     Display *display;
-     Window window;
+initialize_cut_buffers (Display *display, Window window)
 {
   unsigned char *data = (unsigned char *) "";
   BLOCK_INPUT;
@@ -2359,7 +2328,7 @@ DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
 {
   Window window;
   Atom buffer_atom;
-  unsigned char *data;
+  unsigned char *data = NULL;
   int bytes;
   Atom type;
   int format;
@@ -2370,6 +2339,10 @@ DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
   struct frame *sf = SELECTED_FRAME ();
 
   check_x ();
+
+  if (! FRAME_X_P (sf))
+    return Qnil;
+
   display = FRAME_X_DISPLAY (sf);
   dpyinfo = FRAME_X_DISPLAY_INFO (sf);
   window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
@@ -2378,8 +2351,12 @@ DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
 
   x_get_window_property (display, window, buffer_atom, &data, &bytes,
                         &type, &format, &size, 0);
+
   if (!data || !format)
-    return Qnil;
+    {
+      xfree (data);
+      return Qnil;
+    }
 
   if (format != 8 || type != XA_STRING)
     signal_error ("Cut buffer doesn't contain 8-bit data",
@@ -2410,6 +2387,10 @@ DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
   struct frame *sf = SELECTED_FRAME ();
 
   check_x ();
+
+  if (! FRAME_X_P (sf))
+    return Qnil;
+
   display = FRAME_X_DISPLAY (sf);
   window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
 
@@ -2457,8 +2438,8 @@ DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
 
 DEFUN ("x-rotate-cut-buffers-internal", Fx_rotate_cut_buffers_internal,
        Sx_rotate_cut_buffers_internal, 1, 1, 0,
-       doc: /* Rotate the values of the cut buffers by the given number of step.
-Positive means shift the values forward, negative means backward.  */)
+       doc: /* Rotate the values of the cut buffers by N steps.
+Positive means shift the values forward, negative means backward.  */)
      (n)
      Lisp_Object n;
 {
@@ -2466,8 +2447,12 @@ Positive means shift the values forward, negative means backward.  */)
   Atom props[8];
   Display *display;
   struct frame *sf = SELECTED_FRAME ();
-
+  
   check_x ();
+
+  if (! FRAME_X_P (sf))
+    return Qnil;
+
   display = FRAME_X_DISPLAY (sf);
   window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
   CHECK_NUMBER (n);
@@ -2503,8 +2488,7 @@ Positive means shift the values forward, negative means backward.  */)
    bit parts of a 32 bit number).  */
 
 int
-x_check_property_data (data)
-     Lisp_Object data;
+x_check_property_data (Lisp_Object data)
 {
   Lisp_Object iter;
   int size = 0;
@@ -2537,11 +2521,7 @@ x_check_property_data (data)
    XClientMessageEvent).  */
 
 void
-x_fill_property_data (dpy, data, ret, format)
-     Display *dpy;
-     Lisp_Object data;
-     void *ret;
-     int format;
+x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
 {
   long val;
   long  *d32 = (long  *) ret;
@@ -2593,12 +2573,7 @@ x_fill_property_data (dpy, data, ret, format)
    Also see comment for selection_data_to_lisp_data above.  */
 
 Lisp_Object
-x_property_data_to_lisp (f, data, type, format, size)
-     struct frame *f;
-     unsigned char *data;
-     Atom type;
-     int format;
-     unsigned long size;
+x_property_data_to_lisp (struct frame *f, unsigned char *data, Atom type, int format, long unsigned int size)
 {
   return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
                                       data, size*format/8, type, format);
@@ -2607,10 +2582,7 @@ x_property_data_to_lisp (f, data, type, format, size)
 /* Get the mouse position in frame relative coordinates.  */
 
 static void
-mouse_position_for_drop (f, x, y)
-     FRAME_PTR f;
-     int *x;
-     int *y;
+mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
 {
   Window root, dummy_window;
   int dummy;
@@ -2681,7 +2653,7 @@ If the value is 0 or the atom is not known, return the empty string.  */)
     ret = make_string (name, strlen (name));
 
   if (atom && name) XFree (name);
-  if (NILP (ret)) ret = make_string ("", 0);
+  if (NILP (ret)) ret = empty_unibyte_string;
 
   UNBLOCK_INPUT;
 
@@ -2732,11 +2704,7 @@ FRAME is on.  If FRAME is nil, the selected frame is used.  */)
 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT.  */
 
 int
-x_handle_dnd_message (f, event, dpyinfo, bufp)
-     struct frame *f;
-     XClientMessageEvent *event;
-     struct x_display_info *dpyinfo;
-     struct input_event *bufp;
+x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
 {
   Lisp_Object vec;
   Lisp_Object frame;
@@ -2768,15 +2736,15 @@ x_handle_dnd_message (f, event, dpyinfo, bufp)
     }
 
   vec = Fmake_vector (make_number (4), Qnil);
-  AREF (vec, 0) = SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
-                                                 event->message_type));
-  AREF (vec, 1) = frame;
-  AREF (vec, 2) = make_number (event->format);
-  AREF (vec, 3) = x_property_data_to_lisp (f,
-                                           data,
-                                           event->message_type,
-                                           event->format,
-                                           size);
+  ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
+                                              event->message_type)));
+  ASET (vec, 1, frame);
+  ASET (vec, 2, make_number (event->format));
+  ASET (vec, 3, x_property_data_to_lisp (f,
+                                        data,
+                                        event->message_type,
+                                        event->format,
+                                        size));
 
   mouse_position_for_drop (f, &x, &y);
   bufp->kind = DRAG_N_DROP_EVENT;
@@ -2908,7 +2876,7 @@ are ignored.  */)
 
 \f
 void
-syms_of_xselect ()
+syms_of_xselect (void)
 {
   defsubr (&Sx_get_selection_internal);
   defsubr (&Sx_own_selection_internal);
@@ -2976,30 +2944,6 @@ This hook doesn't let you change the behavior of Emacs's selection replies,
 it merely informs you that they have happened.  */);
   Vx_sent_selection_functions = Qnil;
 
-  DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
-              doc: /* Coding system for communicating with other X clients.
-
-When sending text via selection and clipboard, if the requested
-data-type is not "UTF8_STRING", the text is encoded by this coding
-system.
-
-When receiving text, if the data-type of the received text is not
-"UTF8_STRING", it is decoded by this coding system.
-
-See also the documentation of the variable `x-select-request-type' how
-to control which data-type to request for receiving text.
-
-The default value is `compound-text-with-extensions'.  */);
-  Vselection_coding_system = intern ("compound-text-with-extensions");
-
-  DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
-              doc: /* Coding system for the next communication with other X clients.
-Usually, `selection-coding-system' is used for communicating with
-other X clients.  But, if this variable is set, it is used for the
-next communication only.  After the communication, this variable is
-set to nil.  */);
-  Vnext_selection_coding_system = Qnil;
-
   DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
              doc: /* Number of milliseconds to wait for a selection reply.
 If the selection owner doesn't reply in this time, we give up.
@@ -3007,38 +2951,38 @@ A value of 0 means wait as long as necessary.  This is initialized from the
 \"*selectionTimeout\" resource.  */);
   x_selection_timeout = 0;
 
-  QPRIMARY   = intern ("PRIMARY");     staticpro (&QPRIMARY);
-  QSECONDARY = intern ("SECONDARY");   staticpro (&QSECONDARY);
-  QSTRING    = intern ("STRING");      staticpro (&QSTRING);
-  QINTEGER   = intern ("INTEGER");     staticpro (&QINTEGER);
-  QCLIPBOARD = intern ("CLIPBOARD");   staticpro (&QCLIPBOARD);
-  QTIMESTAMP = intern ("TIMESTAMP");   staticpro (&QTIMESTAMP);
-  QTEXT      = intern ("TEXT");        staticpro (&QTEXT);
-  QCOMPOUND_TEXT = intern ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
-  QUTF8_STRING = intern ("UTF8_STRING"); staticpro (&QUTF8_STRING);
-  QDELETE    = intern ("DELETE");      staticpro (&QDELETE);
-  QMULTIPLE  = intern ("MULTIPLE");    staticpro (&QMULTIPLE);
-  QINCR      = intern ("INCR");                staticpro (&QINCR);
-  QEMACS_TMP = intern ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
-  QTARGETS   = intern ("TARGETS");     staticpro (&QTARGETS);
-  QATOM             = intern ("ATOM");         staticpro (&QATOM);
-  QATOM_PAIR = intern ("ATOM_PAIR");   staticpro (&QATOM_PAIR);
-  QNULL             = intern ("NULL");         staticpro (&QNULL);
-  Qcompound_text_with_extensions = intern ("compound-text-with-extensions");
+  QPRIMARY   = intern_c_string ("PRIMARY");    staticpro (&QPRIMARY);
+  QSECONDARY = intern_c_string ("SECONDARY");  staticpro (&QSECONDARY);
+  QSTRING    = intern_c_string ("STRING");     staticpro (&QSTRING);
+  QINTEGER   = intern_c_string ("INTEGER");    staticpro (&QINTEGER);
+  QCLIPBOARD = intern_c_string ("CLIPBOARD");  staticpro (&QCLIPBOARD);
+  QTIMESTAMP = intern_c_string ("TIMESTAMP");  staticpro (&QTIMESTAMP);
+  QTEXT      = intern_c_string ("TEXT");       staticpro (&QTEXT);
+  QCOMPOUND_TEXT = intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
+  QUTF8_STRING = intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING);
+  QDELETE    = intern_c_string ("DELETE");     staticpro (&QDELETE);
+  QMULTIPLE  = intern_c_string ("MULTIPLE");   staticpro (&QMULTIPLE);
+  QINCR      = intern_c_string ("INCR");               staticpro (&QINCR);
+  QEMACS_TMP = intern_c_string ("_EMACS_TMP_");        staticpro (&QEMACS_TMP);
+  QTARGETS   = intern_c_string ("TARGETS");    staticpro (&QTARGETS);
+  QATOM             = intern_c_string ("ATOM");                staticpro (&QATOM);
+  QATOM_PAIR = intern_c_string ("ATOM_PAIR");  staticpro (&QATOM_PAIR);
+  QNULL             = intern_c_string ("NULL");                staticpro (&QNULL);
+  Qcompound_text_with_extensions = intern_c_string ("compound-text-with-extensions");
   staticpro (&Qcompound_text_with_extensions);
 
 #ifdef CUT_BUFFER_SUPPORT
-  QCUT_BUFFER0 = intern ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
-  QCUT_BUFFER1 = intern ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
-  QCUT_BUFFER2 = intern ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
-  QCUT_BUFFER3 = intern ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
-  QCUT_BUFFER4 = intern ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
-  QCUT_BUFFER5 = intern ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
-  QCUT_BUFFER6 = intern ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
-  QCUT_BUFFER7 = intern ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
+  QCUT_BUFFER0 = intern_c_string ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
+  QCUT_BUFFER1 = intern_c_string ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
+  QCUT_BUFFER2 = intern_c_string ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
+  QCUT_BUFFER3 = intern_c_string ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
+  QCUT_BUFFER4 = intern_c_string ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
+  QCUT_BUFFER5 = intern_c_string ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
+  QCUT_BUFFER6 = intern_c_string ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
+  QCUT_BUFFER7 = intern_c_string ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
 #endif
 
-  Qforeign_selection = intern ("foreign-selection");
+  Qforeign_selection = intern_c_string ("foreign-selection");
   staticpro (&Qforeign_selection);
 }