2
votes

I am using Gnu Emacs 24.3 on Ubuntu 12.04. I would like to use (yank) directly on text previously selected by mouse without first calling (kill-ring-save). Say I mark the text "test" in my current Emacs buffer. Then the region is highlighted (according to transient-mark-mode). Then I move the mouse to another position in the buffer. I could now have pressed the middle button to insert the selected text. But this is not what I would like to achieve. Rather I would like to call a function that inserts text that uses the selection. Let's say this function is bound to F1. So I press the left mouse button instead, but now the active region (selected text) is deselected and lost. So when I press F1 how can I access the selected text?

I tried inserting this in my ~/.emacs:

(setq x-select-enable-primary t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)

And it seems to work almost. It does not work imediately, but after calling a function that inserts something into the kill ring, for instance (kill-line), it seems to work..

2

2 Answers

4
votes

You have enabled both x-select-enable-clipboard defaulting to t and x-select-enable-primary which you have set to t. Therefore, the value of the clipboard and that one of the primary selection are put both onto the kill ring in that order both requested from X11 in function x-selection-value.

The clipboard wins since the return value of x-selection-value is

(or clip-text primary-text)

with the obvious meaning of the variables. But, if the text of the clipboard remains the same in two successive calls of x-selection-value then clip-text is set to nil. The last value of clip-text is stored in x-last-selected-text-clipboard. The start value of x-last-selected-text-clipboard is nil.

Therefore, the first time you call x-selection-value (indirectly through yank) you get the value of the clipboard even if you have set the primary selection with the mouse in emacs.

If you remain in emacs afterwards it even does not matter what is used -- the clipboard or the primary selection. You get the copied text. But, if you copy some text to the clipboard in an external application then your next yank in emacs is this text even if you mouse-select something else in emacs. Again, the clipboard wins over the primary selection.

Setting

(setq x-select-enable-clipboard nil)

would solve your problem.


If you want to get copied text from some application which only sets the clipboard then the above method is not appropriate. Maybe, a better alternative would be in this case to age the clipboard when text is selected with the mouse:

(defadvice mouse-set-region (before age-x-clipboard activate)
  (when (and x-select-enable-clipboard x-select-enable-primary)
    (let (x-select-enable-primary) ;; Do not touch primary selection.
      (x-selection-value))))

I thought that your actual problem would be the mix of two different copy-paste methods. You want to copy per primary selection (pure mouse selection method) and you want to paste via the clipboard (copy-yank via keyboard). But, meanwhile I noticed that firefox puts everything also onto the primary selection. The above workaround works as long as all programs you use put the stuff copied with C-c also on the primary.

1
votes

If you have xsel installed:

M-! xsel -o