Fork me on GitHub

Caffeinated Bitstream

Bits, bytes, and words.

GNOME Terminal friendly clipboard patch March 15, 2009

The presence of multiple "clipboard" selection buffers in the X Window System has long been a source of irritation with me, and I frequently end up pasting the wrong thing. A few years ago, I hacked the xterm terminal emulator program to always copy into both buffers, and this solved the problem for me. However, I've been playing around with GNOME Terminal lately, so I recently patched its source code to do something similar.

Background

The root of the problem is that there are two different "selection buffers" that are commonly used in the X Window System:

CLIPBOARD
The "CLIPBOARD" selection buffer is used for the cut-and-paste functions that most users are familiar with: Selecting the "cut", "copy" or "paste" menu items from the application's "Edit" menu, or using the corresponding CTRL-X, CTRL-C, or CTRL-V shortcut keys. This selection buffer is the standard means of performing cut-and-paste operations in most modern applications. However, this selection buffer is unfortunately not used when you merely highlight some text in the GNOME Terminal.
PRIMARY
The "PRIMARY" selection buffer receives data when the user highlights text with the mouse. The text in this buffer is pasted when the user presses the middle mouse button in an application's text entry field. This cut-and-paste buffer is a legacy function which new users are generally not told about, in the interests of avoiding mass confusion. Most modern applications support this buffer.

The annoyance is that text highlighted in GNOME Terminal (and thus stored in PRIMARY) can be pasted into another application by clicking the middle mouse button, but cannot be pasted with CTRL-V. Likewise, text copied from the terminal with SHIFT-CTRL-C (or Edit->Copy) might not be available for pasting with the middle mouse button. I frequently get into situations where I have one bit of text in PRIMARY, and another in CLIPBOARD, and I inevitably use the wrong paste function.

A patch for GNOME Terminal 2.22.3

The attached patch modifies the terminal source code to always copy selected text into CLIPBOARD shortly after it has been copied into PRIMARY. Thus, when you highlight a bit of text in the terminal, it can be pasted into another application with either the middle mouse button or CTRL-V.

This patch is for the GNOME Terminal 2.22.3 source code. I'm sure the patch won't work on more recent versions because they've rearranged a few things, but the basic concept should still work. The patch for xterm I wrote a few years ago is also still available.

Comments:

Very cool. This is actually how XFCE's Terminal behaves by default. Very handy for doing things like right clicking on a URL in your terminal window, choosing copy then middle clicking over Firefox to load it up. I'm guessing the GNOME folks would be opposed to this however as I think it violates some X Windows programming best practices to overwrite both clipboards.... That said, are you aware of a GNOME bug filed for this issue?

Posted by Ray Van Dolson on November 09, 2009 at 09:56 AM MST #

I understand the patch doesn't work for version 2.32.0. Is there any way I can get this working on 2.32 or should I roll back?

Posted by Ray on November 08, 2010 at 01:58 PM MST #

Possible patch for 2.32.0 I'm using it on ubuntu 10.10 and it seems to work ok.. not sure why the delay was important ("shortly after") so in my laziness I didn't implement a callback delay.. I've also not figured out how I can adjust gnome terminal to paste on middle click from the clipboard.. CTRL+SHIFT+V is a proper pain.. the LibVTE doc seems be wrong too
--- gnome-terminal-2.32.0/src/terminal-window.c	2011-07-06 13:15:32.649627035 +0100
+++ gnome-terminal-2.32.0-copypatch/src/terminal-window.c	2011-07-06 13:15:04.493355357 +0100
@@ -965,6 +965,9 @@
 
   action = gtk_action_group_get_action (priv->action_group, "EditCopy");
   gtk_action_set_sensitive (action, can_copy);
+
+  if (can_copy && VTE_IS_TERMINAL(priv->active_screen) && vte_terminal_get_has_selection(VTE_TERMINAL(priv->active_screen)))
+    vte_terminal_copy_clipboard(VTE_TERMINAL(priv->active_screen));
 }
 
 static void

Posted by cpitch on July 06, 2011 at 06:28 AM MDT #

cpitch, thank you for your patch - worked fine for my 2.30.1. However i found that if i build my gnome-terminal-2.30.1 from source, the resulting binary doesn't do menubar and tabs... Could you please advice, if possible, how to fix this?

Posted by vegus on September 12, 2011 at 11:14 PM MDT #

That wouldn't be as a result of this patch.. It is more likely due to a patch you've not applied or a configure option left out. When OS vendors (like canonical) make packages for their oses (ubuntu) they often apply their own patches to the stock source code. When I re-made my ubuntu version, I had to apply these patches to the source code before applying my patch to make my version as close to the stock ubuntu version as possible. It depends which OS you are using as to where you can find this patches

Posted by cpitch on September 13, 2011 at 12:42 AM MDT #

cpitch, sure, the issue is not your patch related, sorry for the confising. Of course, I've checked configure --help output, but didn't see something menu-related, maybe some tricky includes should be specifyed? ; -) i'm using gnome-2.30 under OpenSuse-11.3, gnome-related sources got from the openSUSE-11.3-Oss repo, but gnome-terminal-2.30.1 sources i got from ftp://ftp.gnome.org/pub/gnome/sources/gnome-terminal/2.30/gnome-terminal-2.30.1.tar.gz BTW, another strange thing i've found is that if you try to run your patched fresh-built-from-source gnome-terminal under unpatched one, you get running unpatched version, even when all the terminals had been completely restarted before the run, and vise-versa... very confusing :-\ and it runs as expected from, say, xterm... Thank you for your advice on vendor patches - will try to play with them =)

Posted by vegus on September 13, 2011 at 05:48 AM MDT #

Suse back port a lot of stuff, so it is very possibly a vendor patch that is causing you the problems.. I'm working with a number of suse servers, so I'll have a look at what we have here, see if I can spot something.. Gnome-terminal runs as a multi-window app, a bit like firefox.. When you open a new terminal window, gnome instructs the already running copy of gnome-terminal to open a new window.. in fact if you run gnome terminal on the command line, it will try and talk to an existing copy and tell that one to open a window rather than doing it itself!! Use "gnome-terminal --disable-factory" to run a new copy, this will force a new copy of gnome-terminal to start and you can use this to test the new binary.. when you've finished, you'll need to close ALL the original windows to kill the original copy then open it up again with the new binary.. tricky fun, ey!

Posted by cpitch on September 13, 2011 at 07:00 AM MDT #

cpitch, yep, i've had a look through the whole src/terminal-window.c, noticed priv->active_screen method call almost everywhere :-) Thank you for your explanation of the terminals messaging arch! - now it's clear for me.

Posted by vegus on September 14, 2011 at 09:06 AM MDT #

Thank you for the great explanation and patch.

Posted by Bryan on November 02, 2011 at 09:50 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed