Function: other-buffer

other-buffer is a function defined in buffer.c.

Signature

(other-buffer &optional BUFFER VISIBLE-OK FRAME)

Documentation

Return most recently selected buffer other than BUFFER.

Buffers not visible in windows are preferred to visible buffers, unless optional second argument VISIBLE-OK is non-nil. Ignore the argument BUFFER unless it denotes a live buffer. If the optional third argument FRAME specifies a live frame, then use that frame's buffer list instead of the selected frame's buffer list. Do not return a hidden buffer -- a buffer whose name starts with a space.

The buffer is found by scanning the selected or specified frame's buffer list first, followed by the list of all buffers. If no other buffer exists, return the buffer *scratch* (creating it if necessary).

View in manual

Probably introduced at or before Emacs version 16.

Source Code

// Defined in /usr/src/emacs/src/buffer.c
{
  struct frame *f = decode_live_frame (frame);
  Lisp_Object tail = f->buffer_list, pred = f->buffer_predicate;
  Lisp_Object buf, notsogood = Qnil;

  /* Consider buffers that have been seen in the frame first.  */
  for (; CONSP (tail); tail = XCDR (tail))
    {
      buf = XCAR (tail);
      if (candidate_buffer (buf, buffer)
	  /* If the frame has a buffer_predicate, disregard buffers that
	     don't fit the predicate.  */
	  && (NILP (pred) || !NILP (calln (pred, buf))))
	{
	  if (!NILP (visible_ok)
	      || NILP (Fget_buffer_window (buf, Qvisible)))
	    return buf;
	  else if (NILP (notsogood))
	    notsogood = buf;
	}
    }

  /* Consider alist of all buffers next.  */
  FOR_EACH_LIVE_BUFFER (tail, buf)
    {
      if (candidate_buffer (buf, buffer)
	  /* If the frame has a buffer_predicate, disregard buffers that
	     don't fit the predicate.  */
	  && (NILP (pred) || !NILP (calln (pred, buf))))
	{
	  if (!NILP (visible_ok)
	      || NILP (Fget_buffer_window (buf, Qvisible)))
	    return buf;
	  else if (NILP (notsogood))
	    notsogood = buf;
	}
    }

  if (!NILP (notsogood))
    return notsogood;
  else
    return safe_calln (Qget_scratch_buffer_create);
}