将已抽取的文本保存到Emacs中的字符串

时间:2014-04-09 10:38:53

标签: emacs elisp

我正在尝试将抽取的文本存储到Emacs中的变量。

以下似乎有效:

(let ((str nil))
  (with-temp-buffer
    (yank)
    (setq str (buffer-string)))

我想知道,有没有更简单的方法来实现这一目标?似乎打开一个临时缓冲区只是为了让被拉出的文本过度。

2 个答案:

答案 0 :(得分:9)

您在函数中寻找的值可以

获得
(car kill-ring)

这应该有效:

(defun was-yanked ()
  "When called after a yank, store last yanked value in let-bound yanked. "
  (interactive)
  (let (yanked)
    (and (eq last-command 'yank)
         (setq yanked (car kill-ring))))

也许留言并退还:

(defun was-yanked ()
  "When called after a yank, store last yanked value in let-bound yanked. "
  (interactive)
  (let (yanked)
    (and (eq last-command 'yank)
         (setq yanked (car kill-ring))))
  (when (interactive-p) (message "%s" yanked))
  yanked)

答案 1 :(得分:4)

您可能希望使用 (current-kill 0) 而不是(car kill-ring)

请参阅kill-ring的文档字符串:

,----
| List of killed text sequences.
| Since the kill ring is supposed to interact nicely with cut-and-paste
| facilities offered by window systems, use of this variable should
| interact nicely with `interprogram-cut-function' and
| `interprogram-paste-function'.  The functions `kill-new',
| `kill-append', and `current-kill' are supposed to implement this
| interaction; you may want to use them instead of manipulating the kill
| ring directly.
,----

这也可以从您的评论中回答您的第二个问题。请参阅doc以获取此doc字符串中提到的函数等。