[ACCEPTED]-How can I check if a current buffer exists in Emacs?-elisp
From the documentation:
(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.
This is what I did:
(when (get-buffer "*scratch*") (kill-buffer "*scratch*"))
This checks for the buffer 2 scratch. If there's such a thing, kill it. If 1 not, do nothing at all.
not sure about version this predicate appeared, but 1 now Emacs has
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.
If you'd like to define your hypothetical 4 function as above, this works:
(defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
I use this 3 to automatically close the
*scratch* buffer on startup, so 2 I don't have to cycle through it in my list 1 of buffers, as follows:
(defun buffer-exists (bufname) (not (eq nil (get-buffer bufname)))) (if (buffer-exists "*scratch*") (kill-buffer "*scratch*"))
More Related questions