简体   繁体   English

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

[英]How to copy to clipboard in Emacs Lisp

I want to copy a string to the clipboard (not a region of any particular buffer, just a plain string).我想将一个字符串复制到剪贴板(不是任何特定缓冲区的区域,只是一个普通字符串)。 It would be nice if it were also added to the kill-ring.如果它也被添加到杀戮环中就好了。 Here's an example:下面是一个例子:

(copy-to-clipboard "Hello World")

Does this function exist?有这个功能吗? If so, what is it called and how did you find it?如果有,它叫什么名字,你是怎么找到的? Is there also a paste-from-clipboard function?是否还有paste-from-clipboard功能?

I can't seem to find this stuff in the Lisp Reference Manual, so please tell me how you found it.我在 Lisp 参考手册中似乎找不到这些东西,所以请告诉我你是如何找到它的。

You're looking for kill-new .您正在寻找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.

I do this:我这样做:

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

That gets it on the clipboard.这会在剪贴板上得到它。 If you want it on the kill-ring add a kill-region form also.如果你想在 kill-ring 上添加一个kill-region表单。

The command to put your selection on the window system clipboard is x-select-text .将您的选择放在窗口系统剪贴板上的命令是x-select-text You can give it a block of text to remember.你可以给它一段文字来记住。 So a (buffer-substring (point) (mark)) or something should give you what you need to pass to it.所以(buffer-substring (point) (mark))或其他东西应该给你你需要传递给它的东西。 In Joe's answer, you can see the interprogram-cut-function.在乔的回答中,您可以看到程序间剪切功能。 Look that up for how to find this.查找它以了解如何找到它。

In my .emacs file, i use this在我的 .emacs 文件中,我使用这个

(global-set-key "\C-V" 'yank)
(global-set-key "\C-cc" 'kill-ring-save)

I could not use Ctrl-C (or System-copy), but this may be enough in case old habits kick in.我无法使用 Ctrl-C(或 System-copy),但如果旧习惯开始出现,这可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM