Subject: bug#13790: Cannot paste with C-y into Homebrew emacs v24.2.1

From: Josh <[email-masked]>
Date: Wed, 3 Apr 2013 10:07:20 -0700

On Wed, Mar 13, 2013 at 11:04 AM, Stefan Monnier
<[email-masked]> wrote:
>> I'd be happy to contribute it,
>
> Cool.  Could you try and make it into a patch to xclip.el, then?

OK, I took a stab at it and it seems to work correctly under OS X in
-nw mode.  I do not have access to a system running X at the moment so
someone should verify that the original functionality remains intact.

Please also review the change to the define-minor-mode form, where I
replaced the use of terminal-init-xterm-hook with direct calls to
turn-{on,off}-xclip.  I hadn't heard of this hook before and my
impression after a brief investigation is that these terminal-init
hooks are selected based on the value of TERM during initialization
and then run before user init.  If that's so then I don't see how
adding (xclip-mode 1) to users' .emacs as the commentary suggests
could possibly work, since that would cause turn-on-xclip-mode to be
added to a hook that had already run.  I assume I'm missing something
here, but someone more familiar with the init sequence than I am
should review the mode enablement implementation.

Also, if my impression that the specific terminal-init hook is
selected based on TERM is correct then I don't see how the current
implementation could be working for users with TERM set to rxvt,
screen, screen-256color, etc.

I removed the optional `push' argument to xclip-select-text because it
was unused within the function and also because xclip-select-text is a
target of interprogram-cut-function, whose docstring specifies that
its targets are called with exactly one argument.

>> Also, I see that xclip.el resides in the GNU ELPA repository.  Since
>> it's likely that many people using Emacs in -nw mode would like to
>> integrate Emacs kill and yank operations with their operating systems'
>> clipboards, I wonder if it would be worthwhile for some or all of
>> xclip.el's functionality to move into the core.

>I don't think xclip-mode can be enabled by default (at least not at
>this stage), so the only thing we can improve is to bundle xclip.el with
>Emacs, although installing a package from GNU ELPA is simple enough that
>the pressure to do that is not very high.

Enabling xclip-mode by default or at least bundling it with Emacs
seems worthwhile to me because it represents such an improvement to
Emacs' useability for -nw users.  In any case, regardless of whether
or not Emacs ships with xclip.el, mentioning its existence in the
empty kill ring error message encountered by the reporter would spur
users to investigate how to enable or install it.

Note: `xclip.el.orig' below was retrieved from
http://elpa.gnu.org/packages/xclip-1.0.el

--- xclip.el.orig	2012-11-29 08:41:30.000000000 -0800
+++ xclip.el	2013-04-03 08:40:39.000000000 -0700
@@ -28,7 +28,13 @@

 ;;; Code:
 (defvar xclip-program "xclip"
-  "Name of XClip program tool.")
+  "Path to the xclip program which provides an interface to the X clipboard.")
+
+(defvar xclip-ns-copy-program "pbcopy"
+  "Path to the pbcopy program, an interface to the Nextstep/OS X clipboard.")
+
+(defvar xclip-ns-paste-program "pbpaste"
+  "Path to the pbpaste program, an interface to the Nextstep/OS X clipboard.")

 (defvar xclip-select-enable-clipboard t
   "Non-nil means cutting and pasting uses the clipboard.
@@ -51,7 +57,7 @@
       (process-send-string proc data)
       (process-send-eof proc))))

-(defun xclip-select-text (text &optional push)
+(defun xclip-select-text (text)
   "See `x-select-text'."
   (xclip-set-selection 'primary text)
   (setq xclip-last-selected-text-primary text)
@@ -59,6 +65,20 @@
     (xclip-set-selection 'clipboard text)
     (setq xclip-last-selected-text-clipboard text)))

+(defun xclip-ns-paste ()
+  "Return the text on the Nextstep/OS X system pasteboard."
+  (let ((coding-system-for-read 'utf-8))
+    (shell-command-to-string xclip-ns-paste-program)))
+
+(defun xclip-ns-cut (text)
+  "Send TEXT to the Nextstep/OS X system pasteboard."
+  (let ((process-connection-type nil))
+    (let ((proc (start-process "pbcopy" "*Messages*" xclip-ns-copy-program)))
+      (set-process-sentinel proc 'ignore) ;; stifle noise in *Messages*
+      (process-send-string proc text)
+      (process-send-eof proc)))
+  text)
+
 (defun xclip-selection-value ()
   "See `x-cut-buffer-or-selection-value'."
   (when (and (executable-find xclip-program) (getenv "DISPLAY"))
@@ -94,8 +114,13 @@
       (or clip-text primary-text))))

 (defun turn-on-xclip ()
-  (setq interprogram-cut-function 'xclip-select-text)
-  (setq interprogram-paste-function 'xclip-selection-value))
+  (cond ((eq system-type 'darwin)
+         (setq interprogram-cut-function 'xclip-ns-cut
+               interprogram-paste-function 'xclip-ns-paste))
+        ((getenv "DISPLAY")
+         (setq interprogram-cut-function 'xclip-select-text
+               interprogram-paste-function 'xclip-selection-value))
+        (t (error "No interface to operating system clipboard found"))))

 (defun turn-off-xclip ()
   (setq interprogram-cut-function nil)
@@ -103,11 +128,11 @@

 ;;;###autoload
 (define-minor-mode xclip-mode
-  "Minor mode to use the `xclip' program to copy&paste."
+  "Minor mode to integrate operating system clipboards with kills and yanks."
   :global t
   (if xclip-mode
-      (add-hook 'terminal-init-xterm-hook 'turn-on-xclip)
-    (remove-hook 'terminal-init-xterm-hook 'turn-on-xclip)))
+      (turn-on-xclip)
+    (turn-off-xclip)))

 ;;;; ChangeLog: