2

How can I prevent Emacs from ever modifying the OS X clipboard unless I explicitly ask it to?

I've tried all of:

(setq x-select-enable-clipboard nil)
(setq interprogram-cut-function nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)

Which does prevent kill/yank from modifying the clipboard, but selected text is still put on the clipboard.

This is GNU Emacs.app on OS X.

What else should I try?

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • How are you selecting text that is having the side-effect of updating the clipboard? – Brandon Rhodes Apr 23 '14 at 19:46
  • For example, if I triple-click a line to select the whole thing, the line ends up on my OS clipboard (much like it would end up on X11 selection buffer). – David Wolever Apr 23 '14 at 19:47
  • Then I suppose that my own solution to the problem, come to think of it, is that I never to use the mouse with Emacs. But that does not exactly sound like an SO-worthy answer so I'll just leave it here :) – Brandon Rhodes Apr 23 '14 at 19:48
  • Unfortunately it also happens when I use evil's visual mode… which is quite frequently :\ – David Wolever Apr 23 '14 at 19:49

1 Answers1

4

After doing some digging into the same issue, I believe that the issue actually lies in the Emacs x-select-text function, which explicitly ignores the value of x-select-enable-clipboard on NextStep (and OS X is a NextStep).

I've "solved" this problem by replacing x-select-text with a no-op function, then explicitly using ns-{get,set}pasteboard for interprogram{cut,paste}-function:

; Override the default x-select-text function because it doesn't
; respect x-select-enable-clipboard on OS X.
(defun x-select-text (text))
(setq x-select-enable-clipboard nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)

(setq interprogram-cut-function 'ns-set-pasteboard)
(setq interprogram-paste-function 'ns-get-pasteboard)

Here is the original x-select-text code:

(defun x-select-text (text)
  "Select TEXT, a string, according to the window system.

On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard.  If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.

On MS-Windows, make TEXT the current selection.  If
`x-select-enable-clipboard' is non-nil, copy the text to the
clipboard as well.

On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
is not used)."
  (cond ((eq (framep (selected-frame)) 'w32)
         (if x-select-enable-clipboard
             (w32-set-clipboard-data text))
         (setq x-last-selected-text text))
        ((featurep 'ns) ; This is OS X
         ;; Don't send the pasteboard too much text.
         ;; It becomes slow, and if really big it causes errors.
         (ns-set-pasteboard text)
         (setq ns-last-selected-text text))
        (t
         ;; With multi-tty, this function may be called from a tty frame.
         (when (eq (framep (selected-frame)) 'x)
           (when x-select-enable-primary
             (x-set-selection 'PRIMARY text)
             (setq x-last-selected-text-primary text))
           (when x-select-enable-clipboard
             (x-set-selection 'CLIPBOARD text)
             (setq x-last-selected-text-clipboard text))))))
David Wolever
  • 148,955
  • 89
  • 346
  • 502