28.8 The Buffer List

The buffer list is a list of all live buffers. The order of the buffers in this list is based primarily on how recently each buffer has been displayed in a window. Several functions, notably other-buffer, use this ordering. A buffer list displayed for the user also follows this order.

Creating a buffer adds it to the end of the buffer list, and killing a buffer removes it from that list. A buffer moves to the front of this list whenever it is chosen for display in a window (see Switching to a Buffer in a Window) or a window displaying it is selected (see Selecting Windows). A buffer moves to the end of the list when it is buried (see bury-buffer, below). There are no functions available to the Lisp programmer which directly manipulate the buffer list.

In addition to the fundamental buffer list just described, Emacs maintains a local buffer list for each frame, in which the buffers that have been displayed (or had their windows selected) in that frame come first. (This order is recorded in the frame’s buffer-list frame parameter; see Buffer Parameters.) Buffers never displayed in that frame come afterward, ordered according to the fundamental buffer list.

Function: buffer-list &optional frame

This function returns the buffer list, including all buffers, even those whose names begin with a space. The elements are actual buffers, not their names.

If frame is a frame, this returns frame’s local buffer list. If frame is nil or omitted, the fundamental buffer list is used: the buffers appear in order of most recent display or selection, regardless of which frames they were displayed on.

(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 #'buffer-name (buffer-list))
    ⇒ ("buffers.texi" " *Minibuf-1*"
        "buffer.c" "*Help*" "TAGS")

The list returned by buffer-list is constructed specifically; it is not an internal Emacs data structure, and modifying it has no effect on the order of buffers. If you want to change the order of buffers in the fundamental buffer list, here is an easy way:

(defun reorder-buffer-list (new-list)
  (while new-list
    (bury-buffer (car new-list))
    (setq new-list (cdr new-list))))

With this method, you can specify any order for the list, but there is no danger of losing a buffer or adding something that is not a valid live buffer.

To change the order or value of a specific frame’s buffer list, set that frame’s buffer-list parameter with modify-frame-parameters (see Access to Frame Parameters).

Function: other-buffer &optional buffer visible-ok frame

This function returns the first buffer in the buffer list other than buffer. Usually, this is the buffer appearing in the most recently selected window (in frame frame or else the selected frame, see Input Focus), aside from buffer. Buffers whose names start with a space are not considered at all.

If buffer is not supplied (or if it is not a live buffer), then other-buffer returns the first buffer in the selected frame’s local buffer list. (If frame is non-nil, it returns the first buffer in frame’s local buffer list instead.)

If 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 Buffer 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).

Function: last-buffer &optional buffer visible-ok frame

This function returns the last buffer in frame’s buffer list other than buffer. If frame is omitted or nil, it uses the selected frame’s buffer list.

The argument visible-ok is handled as with other-buffer, see above. If no suitable buffer can be found, the buffer *scratch* is returned.

Command: bury-buffer &optional buffer-or-name

This command 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. The argument can be either a buffer itself or the name of one.

This function operates on each frame’s buffer-list parameter as well as the fundamental buffer list; therefore, the buffer that you bury will come last in the value of (buffer-list frame) and in the value of (buffer-list). In addition, it also puts the buffer at the end of the list of buffers of the selected window (see Window History) provided it is shown in that window.

If buffer-or-name is nil or omitted, this means to bury the current buffer. In addition, if the current buffer is displayed in the selected window (see Selecting Windows), this makes sure that the window is either deleted or another buffer is shown in it. More precisely, if the selected window is dedicated (see Dedicated Windows) and there are other windows on its frame, the window is deleted. If it is the only window on its frame and that frame is not the only frame on its terminal, the frame is dismissed by calling the function specified by frame-auto-hide-function (see Quitting Windows). Otherwise, it calls switch-to-prev-buffer (see Window History) to show another buffer in that window. If buffer-or-name is displayed in some other window, it remains displayed there.

To replace a buffer in all the windows that display it, use replace-buffer-in-windows, See Buffers and Windows.

Command: unbury-buffer

This command switches to the last buffer in the local buffer list of the selected frame. More precisely, it calls the function switch-to-buffer (see Switching to a Buffer in a Window), to display the buffer returned by last-buffer (see above), in the selected window.

Variable: buffer-list-update-hook

This is a normal hook run whenever the buffer list changes. Functions (implicitly) running this hook are get-buffer-create (see Creating Buffers), rename-buffer (see Buffer Names), kill-buffer (see Killing Buffers), bury-buffer (see above), and select-window (see Selecting Windows). This hook is not run for internal or temporary buffers created by get-buffer-create or generate-new-buffer with a non-nil argument inhibit-buffer-hooks.

Functions run by this hook should avoid calling select-window with a nil norecord argument since this may lead to infinite recursion.

Function: buffer-match-p condition buffer-or-name &optional arg

This function checks if a buffer designated by buffer-or-name satisfies the specified condition. Optional third argument arg is passed to the predicate function in condition. A valid condition can be one of the following:

  • A string, interpreted as a regular expression. The buffer satisfies the condition if the regular expression matches the buffer name.
  • A predicate function, which should return non-nil if the buffer matches. If the function expects one argument, it is called with buffer-or-name as the argument; if it expects 2 arguments, the first argument is buffer-or-name and the second is arg (or nil if arg is omitted).
  • A cons-cell (oper . expr) where oper is one of
    (not cond)

    Satisfied if cond doesn’t satisfy buffer-match-p with the same buffer and arg.

    (or conds…)

    Satisfied if any condition in conds satisfies buffer-match-p, with the same buffer and arg.

    (and conds…)

    Satisfied if all the conditions in conds satisfy buffer-match-p, with the same buffer and arg.

    derived-mode

    Satisfied if the buffer’s major mode derives from expr. Note that this condition might fail to report a match if buffer-match-p is invoked before the major mode of the buffer has been established.

    major-mode

    Satisfied if the buffer’s major mode is equal to expr. Prefer using derived-mode instead, when both can work. Note that this condition might fail to report a match if buffer-match-p is invoked before the major mode of the buffer has been established.

  • t Satisfied by any buffer. A convenient alternative to "" (empty string) or (and) (empty conjunction).
Function: match-buffers condition &optional buffer-list arg

This function returns a list of all buffers that satisfy the condition. If no buffers match, the function returns nil. The argument condition is as defined in buffer-match-p above. By default, all the buffers are considered, but this can be restricted via the optional argument buffer-list, which should be a list of buffers to consider. Optional third argument arg will be passed to condition in the same way as buffer-match-p does.