PREV UP NEXT GNU Emacs Lisp Reference Manual

24.8: The Buffer List

The buffer list is a list of all live buffers. Creating a buffer adds it to this list, and killing a buffer deletes it. The order of the buffers in the list is based primarily on how recently each buffer has been displayed in the selected window. Buffers move to the front of the list when they are selected and to the end when they are buried. Several functions, notably other-buffer, use this ordering. A buffer list displayed for the user also follows this order.

Function: buffer-list
This function returns a list of all buffers, including those whose names begin with a space. The elements are actual buffers, not their names.
(buffer-list)
     => (#<buffer buffers.texi>
         #<buffer  *Minibuf-1*> #<buffer buffer.c>
         #<buffer *Help*> #<buffer TAGS>)
;; Note that the name of the minibuffer
;;   begins with a space!
(mapcar (function buffer-name) (buffer-list))
    => ("buffers.texi" " *Minibuf-1*" 
        "buffer.c" "*Help*" "TAGS")

This list is a copy of a list used inside Emacs; modifying it has no effect on the ordering of buffers.

Function: other-buffer &optional buffer-or-name visible-ok
This function returns the first buffer in the buffer list other than buffer-or-name. Usually this is the buffer most recently shown in the selected window, aside from buffer-or-name. Buffers whose names start with a space are not considered.

If buffer-or-name is not supplied (or if it is not a buffer), then other-buffer returns the first buffer on the buffer list that is not visible in any window in a visible frame.

If the selected frame has a non-nil buffer-predicate parameter, then other-buffer uses that predicate to decide which buffers to consider. It calls the predicate once for each buffer, and if the value is nil, that buffer is ignored. See X Frame Parameters.

If visible-ok is nil, other-buffer avoids returning a buffer visible in any window on any visible frame, except as a last resort. If visible-ok is non-nil, then it does not matter whether a buffer is displayed somewhere or not.

If no suitable buffer exists, the buffer *scratch* is returned (and created, if necessary).

Command: bury-buffer &optional buffer-or-name
This function puts buffer-or-name at the end of the buffer list without changing the order of any of the other buffers on the list. This buffer therefore becomes the least desirable candidate for other-buffer to return.

If buffer-or-name is nil or omitted, this means to bury the current buffer. In addition, if the buffer is displayed in the selected window, this switches to some other buffer (obtained using other-buffer) in the selected window. But if the buffer is displayed in some other window, it remains displayed there.

If you wish to replace a buffer in all the windows that display it, use replace-buffer-in-windows. See Buffers and Windows.