How to disable Emacs Evil selection auto-copies to clipboard
Asked Answered
M

3

7

Related to this question: How to disable x paste in emacs

This works for the mouse

(setq mouse-drag-copy-region nil)

How do I do the same for Evil mode in emacs?

I'm on Mac OS, running Emacs 24.2.91.

Mccowyn answered 15/6, 2013 at 19:9 Comment(0)
C
4

Came across this while Googling for a solution to this problem. What I found to work is part of the Spacemacs FAQ:

(fset 'evil-visual-update-x-selection 'ignore)
Cupellation answered 9/7, 2016 at 21:9 Comment(0)
H
3

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))))))
Houston answered 23/4, 2014 at 20:39 Comment(4)
Any reason this answer is better?Mccowyn
It fixes the problem of selected text being put on the keyboard everywhere, not just from Evil.Houston
For me those ns-set-pasteboard, ns-get-pasteboard don't seem to exist so I get errors when cutting text etc. (emacs-mac)Shill
For some reason, ns-set-pasteboard doesn't set the value of ns-last-selected-text. As a result, ns-get-pasteboard immediately overwrites emacs's own selection, which messes up various things. You need to define a new function like (defun my-ns-set-pasteboard (text) (ns-set-pasteboard text) (setq ns-last-selected-text text)) and use that for interprogram-cut-function insetad.Refresh
M
0

I sent this to the evil-mode mailing list:

Problems with Clipboard History and selections

The current implementation of x-select-enable-clipboard calls x-select-text for all cursor movement with a selection. This is not the behavior when using non-evil mode. Basically, emacs takes over my clipboard history unless the below patch is done. I'm on a Mac and use Alfred clipboard history.

My change below in bold seems to fix this issue.

  1. Is this the right thing to do?
  2. Is there a contributions info page that explains how to contribute to this project. I'm familiar with github forks and pull requests.

Thanks,

Justin

(setq x-select-enable-clipboard nil)
(defun evil-visual-update-x-selection (&optional buffer)
  "Update the X selection with the current visual region."
  (let ((buf (or buffer (current-buffer))))
    (when (buffer-live-p buf)
      (with-current-buffer buf
        (when (and (evil-visual-state-p)
                   (fboundp 'x-select-text)
                   (or (not (boundp 'ns-initialized))
                       (with-no-warnings ns-initialized))
                   (not (eq evil-visual-selection 'block)))
          ;; CHANGE
          ;; ONLY call x-select-text if x-select-enable-clipboard is true
          (if x-select-enable-clipboard
              (x-select-text (buffer-substring-no-properties
                              evil-visual-beginning
                              evil-visual-end))))))))
Mccowyn answered 2/1, 2014 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.