【问题标题】:How to copy to clipboard in Emacs Lisp如何在 Emacs Lisp 中复制到剪贴板
【发布时间】:2010-02-01 17:55:12
【问题描述】:

我想将一个字符串复制到剪贴板(不是任何特定缓冲区的区域,只是一个普通字符串)。如果它也被添加到杀戮环中,那就太好了。这是一个例子:

(copy-to-clipboard "Hello World")

这个函数存在吗?如果是这样,它叫什么,你是如何找到它的?还有paste-from-clipboard函数吗?

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

【问题讨论】:

标签: emacs elisp


【解决方案1】:

您正在寻找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.

【讨论】:

  • 看起来很有希望。你是怎么找到这个的?
  • 很多很多年前我自己就需要它。不记得我是怎么找到它的。
  • 实际上,描述说它可以与 kill-ring 一起使用,这是一种内部结构,可能与剪贴板相关,也可能不相关。特别是对我来说,(kill-new "hello") 不会修改剪贴板。
【解决方案2】:

我这样做:

(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-kill is an interactive compiled Lisp function in simple.el'。 (copy-region-as-kill beg end) 像被杀死一样保存区域,但不要杀死它。在瞬态标记模式下,禁用标记。如果interprogram-cut-function' is non-nil, also save the text for a window system cut and paste.
【解决方案3】:

将您的选择放在窗口系统剪贴板上的命令是x-select-text。你可以给它一个文本块来记住。所以(buffer-substring (point) (mark)) 或其他东西应该给你你需要传递给它的东西。在乔的回答中,您可以看到程序间剪切功能。查找它以了解如何找到它。

【讨论】:

    【解决方案4】:

    在我的 .emacs 文件中,我使用了这个

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

    我无法使用 Ctrl-C(或 System-copy),但如果旧习惯出现,这可能就足够了。

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2011-04-27
      • 1970-01-01
      相关资源
      最近更新 更多