6

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.

Community
  • 1
  • 1
justingordon
  • 12,553
  • 12
  • 72
  • 116

3 Answers3

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)
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))))))
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • Any reason this answer is better? – justingordon Apr 26 '14 at 03:24
  • It fixes the problem of selected text being put on the keyboard everywhere, not just from Evil. – David Wolever Apr 26 '14 at 17:51
  • For me those ns-set-pasteboard, ns-get-pasteboard don't seem to exist so I get errors when cutting text etc. (emacs-mac) – Jack Casey May 13 '15 at 04:12
  • 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. – Christian Conkle Sep 14 '15 at 16:37
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))))))))
justingordon
  • 12,553
  • 12
  • 72
  • 116