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