如何在Emacs Lisp中复制到剪贴板

40

我想将一个字符串复制到剪贴板(不是任何特定缓冲区的区域,只是一个普通的字符串)。如果能将它添加到kill-ring,那就更好了。以下是一个示例:

(copy-to-clipboard "Hello World")

这个函数存在吗?如果存在,它被称为什么,你是如何找到它的?还有一个名为paste-from-clipboard的函数吗?

我在Lisp参考手册中似乎找不到这些内容,请告诉我你是如何找到它的。


1
您可能会发现这个问题有帮助:https://dev59.com/JnVD5IYBdhLWcg3wKoWH - Chris Conway
4个回答

65
你需要寻找"kill-new"命令。
kill-new is a compiled Lisp function in `simple.el'.

(kill-new string &optional replace yank-handler)

Make string the latest kill in the kill ring.
Set `kill-ring-yank-pointer' to point to it.
If `interprogram-cut-function' is non-nil, apply it to string.
Optional second argument replace non-nil means that string will replace
the front of the kill ring, rather than being added to the list.

Optional third arguments yank-handler controls how the string is later
inserted into a buffer; see `insert-for-yank' for details.
When a yank handler is specified, string must be non-empty (the yank
handler, if non-nil, is stored as a `yank-handler' text property on string).

When the yank handler has a non-nil PARAM element, the original string
argument is not used by `insert-for-yank'.  However, since Lisp code
may access and use elements from the kill ring directly, the string
argument should still be a "useful" string for such uses.

1
看起来非常有前途。你是怎么发现这个的? - User1
很多年前,我自己也需要它。不记得我是如何找到它的。 - Joe Casadonte
3
描述中实际上说它使用kill-ring,这是一个内部结构,可能与剪贴板有关,也可能没有。对于我个人而言,(kill-new "hello")并不会修改剪贴板。 - Hi-Angel

5

I do this:

(with-temp-buffer
  (insert "Hello World")
  (clipboard-kill-region (point-min) (point-max)))

这将把它复制到剪贴板上。如果你想要它在kill-ring中,也要添加一个kill-region表单。


copy-region-as-kill 可以一次性将文本复制到剪贴板和 kill-ring 中:copy-region-as-killsimple.el 中一个交互式的编译 Lisp 函数。(copy-region-as-kill beg end)将选定区域保存为已删除的文本,但不要删除它。 在瞬时标记模式下,取消标记。 如果 interprogram-cut-function 不为 nil,则还将文本保存到窗口系统的剪切板中。 - Joe Casadonte

0
在我的.emacs文件中,我使用了这个。
(global-set-key "\C-V" 'yank)
(global-set-key "\C-cc" 'kill-ring-save)

我不能使用Ctrl-C(或系统复制),但如果旧习惯发作,这可能已经足够了。


0
将您的选择放在窗口系统剪贴板上的命令是x-select-text。您可以给它一个文本块来记住。因此,(buffer-substring (point) (mark))或类似的东西应该为您提供需要传递给它的内容。在Joe的答案中,您可以看到interprogram-cut-function。查找有关如何找到它的信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接