我想写一个函数,如果已经存在一个给定的缓冲区名称,它会采取行动。例如:
(if (buffer-exists "my-buffer-name")
; do something
)
elisp是否有一个函数来检查是否存在类似于我的“缓冲存在”函数的缓冲区的存在?
由于
答案 0 :(得分:54)
(get-buffer name) Return the buffer named name (a string). If there is no live buffer named name, return nil. name may also be a buffer; if so, the value is that buffer. (get-buffer-create name) Return the buffer named name, or create such a buffer and return it. A new buffer is created if there is no live buffer named name. If name starts with a space, the new buffer does not keep undo information. If name is a buffer instead of a string, then it is the value returned. The value is never nil.
答案 1 :(得分:6)
这就是我所做的:
(when (get-buffer "*scratch*")
(kill-buffer "*scratch*"))
这将检查缓冲区划痕。如果有这样的事情,就杀掉它。 如果没有,什么都不做。
答案 2 :(得分:4)
如果您想定义上述假设函数,则可以使用:
(defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
我使用它在启动时自动关闭*scratch*
缓冲区,所以我不必在缓冲区列表中循环它,如下所示:
(defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
(if (buffer-exists "*scratch*") (kill-buffer "*scratch*"))
答案 3 :(得分:2)
不确定此谓词出现的版本,但现在Emacs已buffer-live-p
:
buffer-live-p is a built-in function in `buffer.c'.
(buffer-live-p OBJECT)
Return non-nil if OBJECT is a buffer which has not been killed.
Value is nil if OBJECT is not a buffer or if it has been killed.