我想在Emacs中将区域复制到另一个(临时)缓冲区,但如果没有(活动的)区域,我想复制整个当前缓冲区。我的做法如下:
(defun do-something-with-region-or-buffer ()
(interactive)
(save-excursion
(let ((begin (point-min)) (end (point-max)))
(when (region-active-p)
(setq begin (region-beginning))
(setq end (region-end)))
(copy-region-as-kill begin end)
(with-temp-buffer
(switch-to-buffer (current-buffer))
(rename-buffer "*My Temp Buffer*")
(delete-other-windows)
(yank)
(do-something-with-current-buffer)))))
然而,我强烈感觉这种做法不够优秀,因为我在操纵kill ring。如何在不影响kill ring的情况下完成操作?(附加问题:如果没有活动区域,我使用区域还是整个缓冲区的方式是否可行?)
with-temp-buffer
:它有点像一个视觉玩具,对当前缓冲区的内容做一些有趣的事情,我希望它在完成后消失。非常感谢!如果没有其他更好的答案出现,我明天左右会接受这个答案。 - mbork