all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Emacs (in terminal): keeping clipboard synchronized with the system global one?
@ 2009-03-06 12:33 Hugo Heden
  2009-03-06 12:55 ` Nikolaj Schumacher
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Hugo Heden @ 2009-03-06 12:33 UTC (permalink / raw)
  To: help-gnu-emacs

Good day all,

When running emacs in a terminal, I have problems synchronizing the
emacs-clipboard with the system clipboard (or the "primary selection",
it doesn't really matter)

When using a regular x-window for emacs, the following works well:

;; emacs.d:
   (global-set-key "\C-w" 'clipboard-kill-region)
   (global-set-key "\M-w" 'clipboard-kill-ring-save)
   (global-set-key "\C-y" 'clipboard-yank)

*But* this has no effect when running emacs within a *terminal*.
Instead, one could use an external tool, "xsel"[1] for keeping the
buffers synchronized. This works well[2]:

;; emacs.d:
 (defun copy-to-x-clipboard ()
   (interactive)
   (with-current-buffer (current-buffer)
   (call-process-region (region-beginning) (region-end) "xsel" nil 0
nil "-p" "-i")))
 (global-set-key "\M-w" 'copy-to-x-clipboard)

Now, I would like to refine this so that M-w is bound to
"copy-to-x-clipboard" whenever emacs is run within a terminal, and
bound to 'clipboard-kill-ring-save when emacs is running within a
normal x-window... or something like that.

Any suggestions? How do you people resolve this?

Best regards
Hugo Heden

--
I am using Ubuntu GNU/Linux 8.10 with
$ emacs-snapshot --version
GNU Emacs 23.0.60.1

[1] http://www.vergenet.net/~conrad/software/xsel/
[2] http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/#comment-5806




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the system global one?
  2009-03-06 12:33 Emacs (in terminal): keeping clipboard synchronized with the system global one? Hugo Heden
@ 2009-03-06 12:55 ` Nikolaj Schumacher
  2009-03-06 21:55   ` Hugo Heden
  2009-03-08 10:05 ` Hugo Heden
       [not found] ` <mailman.2690.1236506736.31690.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 8+ messages in thread
From: Nikolaj Schumacher @ 2009-03-06 12:55 UTC (permalink / raw)
  To: Hugo Heden; +Cc: help-gnu-emacs

Hugo Heden <hugoheden@gmail.com> wrote:

>  (global-set-key "\M-w" 'copy-to-x-clipboard)
>
> Now, I would like to refine this so that M-w is bound to
> "copy-to-x-clipboard" whenever emacs is run within a terminal

(unless (window-system)
  (global-set-key "\M-w" 'copy-to-x-clipboard))

regards,
Nikolaj Schumacher




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the  system global one?
  2009-03-06 12:55 ` Nikolaj Schumacher
@ 2009-03-06 21:55   ` Hugo Heden
  0 siblings, 0 replies; 8+ messages in thread
From: Hugo Heden @ 2009-03-06 21:55 UTC (permalink / raw)
  To: Nikolaj Schumacher; +Cc: help-gnu-emacs

On Fri, Mar 6, 2009 at 1:55 PM, Nikolaj Schumacher <me@nschum.de> wrote:
> Hugo Heden <hugoheden@gmail.com> wrote:
>
>>  (global-set-key "\M-w" 'copy-to-x-clipboard)
>>
>> Now, I would like to refine this so that M-w is bound to
>> "copy-to-x-clipboard" whenever emacs is run within a terminal
>
> (unless (window-system)
>  (global-set-key "\M-w" 'copy-to-x-clipboard))

Ah, that would be:

(defun copy-to-x-clipboard ()
  (interactive)
  (with-current-buffer (current-buffer)
  (call-process-region (region-beginning) (region-end) "xsel" nil 0
nil "-p" "-i")))
(unless (window-system)
 (global-set-key "\M-w" 'copy-to-x-clipboard))

Thanks, that works excellently!

How would I write a corresponding defun that uses xsel when it does
*kill-region*, C-w?

Can I reuse the copy-to-x-clipboard function, and write something like
this (if you see what I mean):

(unless (window-system)
( global-set-key "\C-w"
  copy-to-x-clipboard
      and-then
   erase-region
))

?

Thanks in advance

Hugo Heden




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the  system global one?
  2009-03-06 12:33 Emacs (in terminal): keeping clipboard synchronized with the system global one? Hugo Heden
  2009-03-06 12:55 ` Nikolaj Schumacher
@ 2009-03-08 10:05 ` Hugo Heden
       [not found] ` <mailman.2690.1236506736.31690.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Hugo Heden @ 2009-03-08 10:05 UTC (permalink / raw)
  To: help-gnu-emacs

On Fri, Mar 6, 2009 at 1:33 PM, Hugo Heden <hugoheden@gmail.com> wrote:
> Good day all,
>
> When running emacs in a terminal, I have problems synchronizing the
> emacs-clipboard with the system clipboard (or the "primary selection",
> it doesn't really matter)
>
> When using a regular x-window for emacs, the following works well:
>
> ;; emacs.d:
>   (global-set-key "\C-w" 'clipboard-kill-region)
>   (global-set-key "\M-w" 'clipboard-kill-ring-save)
>   (global-set-key "\C-y" 'clipboard-yank)
>
> *But* this has no effect when running emacs within a *terminal*.
> Instead, one could use an external tool, "xsel"[1] for keeping the
> buffers synchronized.

I've fiddled with this a bit, and the following works well

---

(global-set-key "\C-w" 'clipboard-kill-region)
(global-set-key "\M-w" 'clipboard-kill-ring-save)
(global-set-key "\C-y" 'clipboard-yank)

(unless window-system
  (defun copy-region-to-xsel()
    ;;(with-current-buffer (current-buffer)
    (call-process-region (region-beginning) (region-end) "xsel" nil 0
nil "-p" "-i"))
  ;; )
  (defun ext-kill-ring-save()
    (interactive)
    (copy-region-to-xsel)
    (kill-ring-save (region-beginning) (region-end)))
  (defun ext-kill-region()
    (interactive)
    (copy-region-to-xsel)
    (kill-region (region-beginning) (region-end)))
  (global-set-key "\M-w" 'ext-kill-ring-save)
  (global-set-key "\C-w" 'ext-kill-region))

---

although I would prefer to have the three first lines within an
"if-then-clause" and the rest within an else clause, but I have not
figured out how to do this, I get "Invalid function: (global-set-key
^W (quote clipboard-kill-region))"

(if window-system
(
  (global-set-key "\C-w" 'clipboard-kill-region)
  (global-set-key "\M-w" 'clipboard-kill-ring-save)
  (global-set-key "\C-y" 'clipboard-yank)
)
  (
  ...
   )
)


Sorry to be responding to myself, I just thought the above might be
valuable for future reference. Thank for all the help!

Best regards

Hugo Heden




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the system global one?
       [not found] ` <mailman.2690.1236506736.31690.help-gnu-emacs@gnu.org>
@ 2009-03-08 14:37   ` Miles Bader
  2009-03-08 19:27     ` Hugo Heden
       [not found]     ` <mailman.2725.1236540431.31690.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Miles Bader @ 2009-03-08 14:37 UTC (permalink / raw)
  To: help-gnu-emacs

Hugo Heden <hugoheden@gmail.com> writes:
>   (global-set-key "\M-w" 'ext-kill-ring-save)
>   (global-set-key "\C-w" 'ext-kill-region))

Instead of redefining the keys, you should probably set the variables
`interprogram-cut-function' and `interprogram-paste-function'
(which are used by the default commands).

-Miles

-- 
You can hack anything you want, with TECO and DDT.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the  system global one?
  2009-03-08 14:37   ` Miles Bader
@ 2009-03-08 19:27     ` Hugo Heden
       [not found]     ` <mailman.2725.1236540431.31690.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Hugo Heden @ 2009-03-08 19:27 UTC (permalink / raw)
  To: Miles Bader; +Cc: help-gnu-emacs

On Sun, Mar 8, 2009 at 3:37 PM, Miles Bader <miles@gnu.org> wrote:
> Hugo Heden <hugoheden@gmail.com> writes:
>>   (global-set-key "\M-w" 'ext-kill-ring-save)
>>   (global-set-key "\C-w" 'ext-kill-region))
>
> Instead of redefining the keys, you should probably set the variables
> `interprogram-cut-function' and `interprogram-paste-function'
> (which are used by the default commands).
>
> -Miles
>

Thanks a lot Miles, yes I realized that after a while (as noted on
http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
) -- the following works well:

(global-set-key "\C-w" 'clipboard-kill-region)
(global-set-key "\M-w" 'clipboard-kill-ring-save)
(global-set-key "\C-y" 'clipboard-yank)

(unless window-system
  (defun xsel-cut-function (text &optional push)
    (with-temp-buffer
      (insert text)
      (call-process-region (point-min) (point-max) "xsel" nil 0 nil
"--primary" "--input")))
  (defun xsel-paste-function()
    (let ((xsel-output (shell-command-to-string "xsel -o")))
      (unless (string= (car kill-ring) xsel-output)
	xsel-output )))
  (setq interprogram-cut-function 'xsel-cut-function)
  (setq interprogram-paste-function 'xsel-paste-function)
  )

Best regards

Hugo Heden




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the system global one?
       [not found]     ` <mailman.2725.1236540431.31690.help-gnu-emacs@gnu.org>
@ 2009-03-08 23:13       ` Miles Bader
  2009-03-10 20:22         ` Hugo Heden
  0 siblings, 1 reply; 8+ messages in thread
From: Miles Bader @ 2009-03-08 23:13 UTC (permalink / raw)
  To: help-gnu-emacs

Hugo Heden <hugoheden@gmail.com> writes:
> (global-set-key "\C-w" 'clipboard-kill-region)
> (global-set-key "\M-w" 'clipboard-kill-ring-save)
> (global-set-key "\C-y" 'clipboard-yank)

These rebindings are not necessary either -- you can merely do:

   (setq x-select-enable-clipboard t)

Which will make the standard bindings use the clipboard
(all clipboard-kill-region etc do is call the standard commands with
x-select-enable-clipboard bound to t).

-Miles

-- 
Politeness, n. The most acceptable hypocrisy.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs (in terminal): keeping clipboard synchronized with the  system global one?
  2009-03-08 23:13       ` Miles Bader
@ 2009-03-10 20:22         ` Hugo Heden
  0 siblings, 0 replies; 8+ messages in thread
From: Hugo Heden @ 2009-03-10 20:22 UTC (permalink / raw)
  To: Miles Bader; +Cc: help-gnu-emacs

On Mon, Mar 9, 2009 at 12:13 AM, Miles Bader <miles@gnu.org> wrote:
> Hugo Heden <hugoheden@gmail.com> writes:
>> (global-set-key "\C-w" 'clipboard-kill-region)
>> (global-set-key "\M-w" 'clipboard-kill-ring-save)
>> (global-set-key "\C-y" 'clipboard-yank)
>
> These rebindings are not necessary either -- you can merely do:
>
>   (setq x-select-enable-clipboard t)
>
> Which will make the standard bindings use the clipboard
> (all clipboard-kill-region etc do is call the standard commands with
> x-select-enable-clipboard bound to t).
>

Ah, thanks Miles, that seems cleaner.

Hugo Heden




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-03-10 20:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-06 12:33 Emacs (in terminal): keeping clipboard synchronized with the system global one? Hugo Heden
2009-03-06 12:55 ` Nikolaj Schumacher
2009-03-06 21:55   ` Hugo Heden
2009-03-08 10:05 ` Hugo Heden
     [not found] ` <mailman.2690.1236506736.31690.help-gnu-emacs@gnu.org>
2009-03-08 14:37   ` Miles Bader
2009-03-08 19:27     ` Hugo Heden
     [not found]     ` <mailman.2725.1236540431.31690.help-gnu-emacs@gnu.org>
2009-03-08 23:13       ` Miles Bader
2009-03-10 20:22         ` Hugo Heden

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.