[feature proposal] save selection in CUT_BUFFER0 on close

Andrei Paskevich andrei at capet.iut-fbleau.fr
Sat Jan 20 14:40:08 CET 2007


Hello. I discovered today that selection in an urxvt window vanishes 
when that window is closed. Unlikely, both rxvt and xterm copy their
selections in CUT_BUFFER (not on close, but every time something is
selected). I would not qualify this as a bug of urxvt, since my GUI
applications seem to never look into cut_buffers anyway, but keeping 
selection after terminal close seems to be a worthy feature, even if
for nothing but term-to-term copypaste.

I wrote a little patch which adds this feature in the following way:

1. A method 'void selection_drown()' is added to the 'rxvt_term' struct;

2. This method checks that the current window is the selection owner,
   and if so, puts the selection text into CUT_BUFFER0, either as 
   a UTF8_STRING or as a COMPOUND_TEXT. For wchar->utf8 conversion,
   either a standard Xlib routine or `rxvt_wcstoutf8()' can be used.

3. This method is called right before 'selection_clear()' 
   in the destructor method of 'rxvt_term' at (main.C:227).

   Alternatively, to simulate the behaviour of rxvt and xterm,
   it could be called right after 'set_selection_owner(this)'
   in the 'selection_grab()' method at (screen.C:3069).

After some testing, this seems to work fine (both with rxvt_ and Xlib
utf8 conversions). The saved utf-8 string can be pasted into an urxvt
or standard rxvt window. For some reason, which I don't grasp yet,
my xterm ignores UTF8_STRINGs in the cut buffer, though COMPOUND_TEXT
pastes fine. 

Here comes the patch:
========================== urxvt-drown.diff ==========================
diff -ur rxvt-unicode-8.1/src/main.C rxvt-unicode-070120/src/main.C
--- rxvt-unicode-8.1/src/main.C	2006-12-07 04:18:34.000000000 +0100
+++ rxvt-unicode-070120/src/main.C	2007-01-20 11:32:18.000000000 +0100
@@ -224,6 +224,7 @@
 
   if (display)
     {
+      selection_drown ();
       selection_clear ();
 
 #ifdef USE_XIM
diff -ur rxvt-unicode-8.1/src/rxvt.h rxvt-unicode-070120/src/rxvt.h
--- rxvt-unicode-8.1/src/rxvt.h	2006-12-07 04:26:45.000000000 +0100
+++ rxvt-unicode-070120/src/rxvt.h	2007-01-20 11:03:46.000000000 +0100
@@ -1412,6 +1412,7 @@
   void selection_clear () NOTHROW;
   void selection_make (Time tm);
   bool selection_grab (Time tm) NOTHROW;
+  void selection_drown () NOTHROW;
   void selection_start_colrow (int col, int row) NOTHROW;
   void selection_delimit_word (enum page_dirn dirn, const row_col_t *mark, row_col_t *ret) NOTHROW;
   void selection_extend_colrow (int32_t col, int32_t row, int button3, int buttonpress, int clickchange) NOTHROW;
diff -ur rxvt-unicode-8.1/src/screen.C rxvt-unicode-070120/src/screen.C
--- rxvt-unicode-8.1/src/screen.C	2006-12-07 04:27:35.000000000 +0100
+++ rxvt-unicode-070120/src/screen.C	2007-01-20 13:24:22.000000000 +0100
@@ -3086,6 +3086,40 @@
 #endif
 }
 
+void
+rxvt_term::selection_drown () NOTHROW
+{
+  if (display->selection_owner != this)
+      return;
+
+// '0' means we trust utf8 handling in Xlib
+#if 0 && X_HAVE_UTF8_STRING
+  char *s = rxvt_wcstoutf8 (selection.text, -1);
+
+  XChangeProperty (dpy, display->root, XA_CUT_BUFFER0,
+                   xa[XA_UTF8_STRING], 8, PropModeReplace,
+                   (const unsigned char *)s, strlen (s));
+
+  free(s);
+#else
+  XTextProperty ct;
+# if X_HAVE_UTF8_STRING
+  XICCEncodingStyle st = XUTF8StringStyle;
+# else
+  XICCEncodingStyle st = XStdICCTextStyle;
+# endif
+
+  if (XwcTextListToTextProperty (dpy, &selection.text, 1, st, &ct) < 0)
+      return;
+
+  XChangeProperty (dpy, display->root, XA_CUT_BUFFER0,
+                   ct.encoding, ct.format, PropModeReplace,
+                   ct.value, ct.nitems);
+
+  XFree (ct.value);
+#endif
+}
+
 /* ------------------------------------------------------------------------- */
 /*
  * Mark or select text based upon number of clicks: 1, 2, or 3
========================== urxvt-drown.diff ==========================

Any comments are highly welcome.

Best regards,
Andrei




More information about the rxvt-unicode mailing list