我想編寫一個函數,該函數在給定緩衝區名稱已經存在的情況下會採取行動。例如:如何檢查Emacs中是否存在當前緩衝區?
(if (buffer-exists "my-buffer-name")
; do something
)
是否elisp的有將檢查對類似我如何由一個緩衝的存在功能「緩衝-存在」功能呢?
感謝
我想編寫一個函數,該函數在給定緩衝區名稱已經存在的情況下會採取行動。例如:如何檢查Emacs中是否存在當前緩衝區?
(if (buffer-exists "my-buffer-name")
; do something
)
是否elisp的有將檢查對類似我如何由一個緩衝的存在功能「緩衝-存在」功能呢?
感謝
(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.
如果您想將其定義爲上述假想的功能,這個工程:
(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*"))
'或'(不是
@Stefan(nitpick)給定的`buffer-exists`與' get-buffer「,因爲它會返回`t`或`nil`,但從不是實際的緩衝區。 – 2017-03-17 05:54:37
這是我做過什麼:
(when (get-buffer "*scratch*")
(kill-buffer "*scratch*"))
此檢查的緩衝區劃傷。如果有這樣的事情,就殺了它。 如果不是,則什麼也不做。
不知道這個版本出現了謂詞,但是現在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.
不應該得到緩衝被用來作爲參數傳遞給bufferp以驗證它確實是一個緩衝區?因爲`get-buffer`返回`nil`或一個緩衝區,所以你可以使用緩衝區緩衝區(緩衝區緩衝區「我的緩衝區名稱」) ;做點什麼 ) – PuercoPop 2012-09-20 17:29:35
不需要使用`bufferp`。直接對它進行測試:`(let((b(get-buffer「foo」)))(if b ...))' – 2012-09-20 20:16:23