Package: xsel
Version: 0.9.6-1
Followup-For: Bug #231413

Here is a second version of the patch. This adds UTF8 support without
breaking things. New command-line option (-u|--utf8) switches between
XA_STRING and UTF8_STRING selection targets. When UTF8_STRING is not
supported by X-server, xsel falls back to XA_STRING.

Please note, this patch also fixes #203925 (manpage bug).

Thanks for advice and testing to Thomas Dickey <[EMAIL PROTECTED]>.

-- System Information:
Debian Release: testing/unstable
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.4.29
Locale: LANG=ru_RU.koi8r, LC_CTYPE=ru_RU.koi8r (charmap=KOI8-R)

Versions of packages xsel depends on:
ii  libc6                2.3.5-6             GNU C Library: Shared libraries an
ii  libice6              4.3.0.dfsg.1-12.0.1 Inter-Client Exchange library
ii  libsm6               4.3.0.dfsg.1-12.0.1 X Window System Session Management
ii  libx11-6             4.3.0.dfsg.1-12.0.1 X Window System protocol client li
ii  libxext6             4.3.0.dfsg.1-12.0.1 X Window System miscellaneous exte
ii  xlibs                6.8.2.dfsg.1-7      X Window System client libraries m

xsel recommends no packages.

-- no debconf information
--- xsel-0.9.6/xsel.1x  2001-07-24 10:43:36.000000000 +0700
+++ xsel-0.9.6utf8/xsel.1x      2005-10-27 10:22:36.000000000 +0700
@@ -80,6 +80,18 @@
 and \fIoutput\fR options.
 
 .PP
+\fBEncoding options\fR
+.TP
+\fB\-u\fR, \fB\-\-utf8\fR 
+Request selection in UTF8 if possible.
+This option is useful when working with selections in national
+(non Latin-1) charsets. If your X-server and target application
+support UTF8_STRING exchange, then selection is returned as
+UTF8 string. If your X-server doesn't support this extension,
+option has no effect. If target application doesn's support
+UTF8 selection target, empty selection is returned.
+
+.PP
 \fBX options\fR
 .TP
 \fB\-d\fR \fIdisplayname\fR, \fB\-\-display\fR \fIdisplayname\fR
@@ -108,7 +120,7 @@
 Print informative messages. Additional instances of \fI-v\fR raise the
 debugging level, ie. print more information.
 .TP
-\fB\-v\fR, \fB\-\-version\fR
+\fB\-\-version\fR
 output version information and exit
 .PP
 .SH NOTES
diff -r -U3 xsel-0.9.6/xsel.c xsel-0.9.6utf8/xsel.c
--- xsel-0.9.6/xsel.c   2001-07-24 10:39:44.000000000 +0700
+++ xsel-0.9.6utf8/xsel.c       2005-10-27 10:02:26.000000000 +0700
@@ -56,11 +56,14 @@
 static Atom incr_atom; /* The INCR atom */
 static Atom null_atom; /* The NULL atom */
 static Atom text_atom; /* The TEXT atom */
+static Atom utf8_atom; /* The UTF8 atom */
+static Atom local_target; /* UTF8_STRING or STRING */
 
 /* Number of selection targets served by this.
- * (MULTIPLE, INCR, TARGETS, TIMESTAMP, DELETE, TEXT and STRING) */
-#define NUM_TARGETS 7
-static Atom supported_targets[NUM_TARGETS];
+ * (MULTIPLE, INCR, TARGETS, TIMESTAMP, DELETE, TEXT, UTF8_STRING and STRING) 
*/
+#define MAX_NUM_TARGETS 8
+static int NUM_TARGETS;
+static Atom supported_targets[MAX_NUM_TARGETS];
 
 /* do_follow: Follow mode for output */
 static Boolean do_follow = False;
@@ -108,6 +111,8 @@
   printf ("  -c, --clear           Clear the selection\n");
   printf ("  -d, --delete          Request that the selection be cleared and 
that\n");
   printf ("                        the application owning it delete its 
contents\n\n");
+  printf ("Encoding options\n");
+  printf ("  -u, --utf8            Request selection in UTF-8 if 
possible\n\n");
   printf ("Selection options\n");
   printf ("  -p, --primary         Operate on the PRIMARY selection 
(default)\n");
   printf ("  -s, --secondary       Operate on the SECONDARY selection\n");
@@ -225,6 +230,7 @@
   if (atom == incr_atom) return "INCR";
   if (atom == null_atom) return "NULL";
   if (atom == text_atom) return "TEXT";
+  if (utf8_atom!=XA_STRING && atom == utf8_atom) return "UTF8_STRING";
   if (atom == XInternAtom (display, "XSEL_DATA", True)) return "XSEL_DATA";
 
   return "<unknown atom>";
@@ -595,7 +601,7 @@
           retval = wait_incr_selection (selection, &event.xselection,
                                         *(int *)value);
           keep_waiting = False;
-        } else if (target != XA_STRING && request_target != delete_atom) {
+        } else if (target != utf8_atom && target != XA_STRING && 
request_target != delete_atom) {
           /* Report non-TEXT atoms */
           print_debug (D_WARN, "Selection (type %s) is not a string.",
                        get_atom_name (target));
@@ -1263,6 +1269,22 @@
 }
 
 /*
+ * handle_utf8_string (display, requestor, property, sel)
+ *
+ * Handle a UTF8_STRING request; setting 'sel' as the data
+ */
+static HandleResult
+handle_utf8_string (Display * display, Window requestor, Atom property,
+               unsigned char * sel, Atom selection, Time time,
+               MultTrack * mparent)
+{
+  return
+    change_property (display, requestor, property, utf8_atom, 8,
+                     PropModeReplace, sel, strlen(sel),
+                     selection, time, mparent);
+}
+
+/*
  * handle_delete (display, requestor, property)
  *
  * Handle a DELETE request.
@@ -1308,6 +1330,9 @@
     } else if (mt->atoms[i] == XA_STRING || mt->atoms[i] == text_atom) {
       retval |= handle_string (mt->display, mt->requestor, mt->atoms[i+1],
                                mt->sel, mt->selection, mt->time, mt);
+    } else if (mt->atoms[i] == utf8_atom) {
+      retval |= handle_utf8_string (mt->display, mt->requestor, mt->atoms[i+1],
+                               mt->sel, mt->selection, mt->time, mt);
     } else if (mt->atoms[i] == delete_atom) {
       retval |= handle_delete (mt->display, mt->requestor, mt->atoms[i+1]);
     } else if (mt->atoms[i] == None) {
@@ -1480,6 +1505,11 @@
     ev.property = xsr->property;
     hr = handle_string (ev.display, ev.requestor, ev.property, sel,
                         ev.selection, ev.time, NULL);
+  } else if (ev.target == utf8_atom) {
+    /* Received UTF8_STRING request */
+    ev.property = xsr->property;
+    hr = handle_utf8_string (ev.display, ev.requestor, ev.property, sel,
+                        ev.selection, ev.time, NULL);
   } else if (ev.target == delete_atom) {
     /* Received DELETE request */
     ev.property = xsr->property;
@@ -1692,8 +1722,8 @@
 {
   unsigned char * text1, * text2;
 
-  text1 = get_selection (XA_PRIMARY, XA_STRING);
-  text2 = get_selection (XA_SECONDARY, XA_STRING);
+  text1 = get_selection (XA_PRIMARY, local_target);
+  text2 = get_selection (XA_SECONDARY, local_target);
 
   set_selection_pair__daemon (text1, text2);
 }
@@ -1710,8 +1740,8 @@
 {
   unsigned char * text1, * text2;
 
-  text1 = get_selection (XA_PRIMARY, XA_STRING);
-  text2 = get_selection (XA_SECONDARY, XA_STRING);
+  text1 = get_selection (XA_PRIMARY, local_target);
+  text2 = get_selection (XA_SECONDARY, local_target);
 
   set_selection_pair__daemon (text2, text1);
 }
@@ -1750,6 +1780,7 @@
   unsigned char * old_sel = NULL, * new_sel = NULL;
   char * display_name = NULL;
   long timeout_ms = 0L;
+  int need_utf8 = False;
 
   progname = argv[0];
 
@@ -1775,6 +1806,8 @@
     } else if (OPT("--append") || OPT("-a")) {
       do_append = True;
       dont_output = True;
+    } else if (OPT("--utf8") || OPT("-u")) {
+      need_utf8 = True;
     } else if (OPT("--input") || OPT("-i")) {
       do_input = True;
       dont_output = True;
@@ -1877,35 +1910,64 @@
   if (test_atom != XA_SECONDARY)
     print_debug (D_WARN, "XA_SECONDARY not named \"SECONDARY\"\n");
 
+  NUM_TARGETS=0;
   /* Get the TIMESTAMP atom */
   timestamp_atom = XInternAtom (display, "TIMESTAMP", False);
   supported_targets[s++] = timestamp_atom;
+  NUM_TARGETS++;
 
   /* Get the MULTIPLE atom */
   multiple_atom = XInternAtom (display, "MULTIPLE", False);
   supported_targets[s++] = multiple_atom;
+  NUM_TARGETS++;
 
   /* Get the TARGETS atom */
   targets_atom = XInternAtom (display, "TARGETS", False);
   supported_targets[s++] = targets_atom;
+  NUM_TARGETS++;
 
   /* Get the DELETE atom */
   delete_atom = XInternAtom (display, "DELETE", False);
   supported_targets[s++] = delete_atom;
+  NUM_TARGETS++;
 
   /* Get the INCR atom */
   incr_atom = XInternAtom (display, "INCR", False);
   supported_targets[s++] = incr_atom;
+  NUM_TARGETS++;
 
   /* Get the NULL atom */
   null_atom = XInternAtom (display, "NULL", False);
+  NUM_TARGETS++;
 
   /* Get the TEXT atom */
   text_atom = XInternAtom (display, "TEXT", False);
   supported_targets[s++] = text_atom;
+  NUM_TARGETS++;
+
+  /* Get the UTF8_STRING atom */
+  utf8_atom = XInternAtom (display, "UTF8_STRING", True);
+  if( utf8_atom != None)
+    {
+    supported_targets[s++] = utf8_atom;
+    NUM_TARGETS++;
+    }
+  else
+    {
+    utf8_atom = XA_STRING;
+    }
 
   supported_targets[s++] = XA_STRING;
+  NUM_TARGETS++;
 
+  if( need_utf8 == True)
+    {
+    local_target=utf8_atom;
+    }
+  else
+    {
+    local_target=XA_STRING;
+    }
   /* handle selection keeping and exit if so */
   if (do_keep) {
     keep_selections ();
@@ -1926,7 +1988,7 @@
   /* handle output modes */
   if (do_output || !dont_output) {
     /* Get the current selection */
-    old_sel = get_selection (selection, XA_STRING);
+    old_sel = get_selection (selection, local_target);
     if (old_sel) printf ("%s", old_sel);
   }
 
@@ -1938,7 +2000,7 @@
   }
   else if (do_input || !dont_input) {
     if (do_append) {
-      if (!old_sel) old_sel = get_selection (selection, XA_STRING);
+      if (!old_sel) old_sel = get_selection (selection, local_target);
       new_sel = copy_sel (old_sel);
     }
     new_sel = initialise_read (new_sel);

Reply via email to