14

在用于 OSX 的 GNU Emacs 中,我怎样才能将杀伤环和 OSX 剪贴板分开?(这样我基本上有两个独立的杀戮戒指。)

使用所需的行为,这将起作用:
1。⌘</kbd>C to copy text from the web to OSX clipboard.
2. controlk to kill a line in Emacs.
3. controly to yank killed text from Emacs kill ring to current Emacs buffer.
4. ⌘</kbd>v to paste original web text from OSX clipboard to current Emacs buffer.

这在 Aquamacs 中开箱即用。如何在 GNU Emacs 中工作?

此处讨论了与 Windows 相关的问题: Emacs:如何将杀伤环与系统剪贴板分开?

在这里: http: //lists.gnu.org/archive/html/help-emacs-windows/2010-02/msg00001.HTML

...但是这个解决方案在 OSX 中不起作用。我想要 Mac OSX 的解决方案。

4

6 回答 6

11

Emacs中的解决方案:如何将kill ring与系统剪贴板分离?确实有效,虽然不完整。您可以打电话给pbcopy自己以正确粘贴剪贴板。例如,在您的.emacs. 请注意,这s-v是针对Cmd+VOS X 窗口系统的。也一样s-c

;;; Tested on:
;;; 1.  GNU Emacs 24.3.1 (x86_64-apple-darwin13.0.0)
;;;     of 2013-12-22 on tennine-slave.macports.org
;;;     (MacPorts emacs@24.3_1)
;;;
;;; 2.  GNU Emacs 24.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
;;;     of 2013-03-12 on bob.porkrind.org
;;;     (Emacs For Mac OS X)

(defun isolate-kill-ring()
  "Isolate Emacs kill ring from OS X system pasteboard.
This function is only necessary in window system."
  (interactive)
  (setq interprogram-cut-function nil)
  (setq interprogram-paste-function nil))

(defun pasteboard-copy()
  "Copy region to OS X system pasteboard."
  (interactive)
  (shell-command-on-region
   (region-beginning) (region-end) "pbcopy"))

(defun pasteboard-paste()
  "Paste from OS X system pasteboard via `pbpaste' to point."
  (interactive)
  (shell-command-on-region
   (point) (if mark-active (mark) (point)) "pbpaste" nil t))

(defun pasteboard-cut()
  "Cut region and put on OS X system pasteboard."
  (interactive)
  (pasteboard-copy)
  (delete-region (region-beginning) (region-end)))

(if window-system
    (progn
      (isolate-kill-ring)
      ;; bind CMD+C to pasteboard-copy
      (global-set-key (kbd "s-c") 'pasteboard-copy)
      ;; bind CMD+V to pasteboard-paste
      (global-set-key (kbd "s-v") 'pasteboard-paste)
      ;; bind CMD+X to pasteboard-cut
      (global-set-key (kbd "s-x") 'pasteboard-cut))

  ;; you might also want to assign some keybindings for non-window
  ;; system usage (i.e., in your text terminal, where the
  ;; command->super does not work)
  )

如果您遇到 UTF-8 问题,请考虑以下可能的解决方案:

;; handle emacs utf-8 input
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
(setenv "LANG" "en_US.UTF-8")
于 2014-06-16T17:39:04.333 回答
1

经过多次摆弄,我很确定使这项工作的唯一方法是覆盖该x-select-text方法。在此处查看我的答案以了解所有详细信息:https ://stackoverflow.com/a/23254771/71522

于 2014-04-23T19:46:24.360 回答
1

注意:这个草案解决方案并不是要对 Emacs 系统范围内的剪贴板进行修改——相反,这是一个自定义解决方案,旨在在专门使用这些自定义功能时才在interactive基础上保持剪贴板分离。Emacs 中使用 的其他函数可以使用类似的方法进行修改——变量和可以在特定函数的持续时间内变为一个值(通过,或修改源本身,或创建新函数和/或使用)。然而,后者超出了这个有限示例的范围。kill-ringinterprogram-cut-functioninterprogram-paste-functionlet-boundniladvicedefalias


历史

初稿(2014 年 12 月 23 日):这是初稿,其理念是仅C-u在调用复制或粘贴功能之前使用 OSX 剪贴板才能访问。如果C-u首先调用,则使用 OSX 剪贴板。随着我每天更多地使用这些功能,我可能会对这段代码进行额外的修改,我会不时更新:

编辑(2014 年 12 月 24 日):*从交互式命令语句中删除lawlist-copy-selected-region- 这是read-only粘贴而不是复制所需的检查。添加了有关此示例的一般性质的声明。

编辑(2014 年 12 月 28 日):修改代码以更好地处理用户在调用lawlist-copy-selected-region. 小修改使代码更简洁。


(defun lawlist-copy-selected-region (&optional arg)
(interactive "P")
  (let* (
      (interprogram-cut-function
        (when (equal arg '(4)) interprogram-cut-function))
      (interprogram-paste-function
        (when (equal arg '(4)) interprogram-paste-function))
      (region-active-p (region-active-p))
      (beg (when region-active-p (region-beginning)))
      (end (when region-active-p (region-end)))
      (copied-string
        (when region-active-p (buffer-substring-no-properties beg end))) )
    (unless region-active-p
      (let ((debug-on-quit nil))
        (signal 'quit `("No region has been selected!"))))
    (copy-region-as-kill beg end)
    (when (not (active-minibuffer-window))
      (message "%s"
        (concat
          (if (and interprogram-cut-function interprogram-paste-function)
            "OSX+Emacs:  "
            "Emacs:  ")
          (truncate-string-to-width copied-string 40)
          (when (> (length copied-string) 40)
            " . . .")))) ))

(defun lawlist-yank (&optional arg)
  (interactive "*P")
  (unless arg (setq arg 1))
  (setq yank-window-start (window-start))
  (setq this-command t)
  (push-mark (point))
  (insert-for-yank
    (lawlist-current-kill
      (cond
        ((listp arg)
          arg)
        ((eq arg '-)
          -2)
        (t
          (1- arg) ))))
  (if (consp arg)
      (goto-char (prog1 (mark t)
       (set-marker (mark-marker) (point) (current-buffer)))))
  (if (eq this-command t)
      (setq this-command 'yank))
  (when (region-active-p)
    (setq mark-active nil))
  nil)

(defun lawlist-current-kill (n &optional do-not-move)
  (let ((interprogram-paste
          (and
            (equal n '(4))
            interprogram-paste-function
            (funcall interprogram-paste-function))))
    (cond
      (interprogram-paste
        (let ((interprogram-cut-function nil))
          (if (listp interprogram-paste)
            (mapc 'kill-new (nreverse interprogram-paste))
            (kill-new interprogram-paste)))
        (car kill-ring))
      ((and (equal n '(4)) (not interprogram-paste))
        (car kill-ring))
      (t
        (or kill-ring 
          (let ((debug-on-quit nil))
            (signal 'quit `("The kill-ring is empty."))))
        (let (
            (ARGth-kill-element
              (nthcdr
                (mod (- n (length kill-ring-yank-pointer)) (length kill-ring))
                kill-ring)))
          (unless do-not-move
            (setq kill-ring-yank-pointer ARGth-kill-element)
            (when
                (and
                  yank-pop-change-selection
                  (> n 0)
                  interprogram-cut-function)
              (funcall interprogram-cut-function (car ARGth-kill-element))))
        (car ARGth-kill-element))))))
于 2014-12-24T06:59:21.833 回答
0
(global-set-key (kbd "C-x M-y")
              (lambda ()
                (interactive)
                (insert-string (ns-get-pasteboard))))

(global-set-key (kbd "C-x M-w")
              (lambda ()
                (interactive)
                (when (region-active-p)
                  (ns-set-pasteboard
                   (buffer-substring (region-beginning)
                                     (region-end))))))
于 2014-11-13T07:12:21.803 回答
0

simpleclip可能会有所帮助-

在 Emacs 中简化对系统剪贴板的访问。

simpleclip-mode 从根本上简化了剪贴板处理:系统剪贴板和 Emacs kill ring 完全独立,不会相互影响。

super 键绑定对 OS X 很友好:super 通常映射到“命令”键,即⌘。

在 OS X、X11 和 MS Windows 上测试

https://github.com/rolandwalker/simpleclip

于 2016-09-21T08:59:51.173 回答
-2

这有帮助吗:

(setq x-select-enable-clipboard nil)

这只会将两个剪贴板分开,并且Cmd+cCmd+v像提到的那样工作,您必须将它们重新绑定到clipboard-kill-ring-saveand clipboard-yank

我正在使用这个 Emacs:https ://github.com/railwaycat/emacs-mac-port

于 2014-04-04T02:16:20.383 回答