Function: record-window-buffer

record-window-buffer is a byte-compiled function defined in window.el.gz.

Signature

(record-window-buffer &optional WINDOW)

Documentation

Record WINDOW's buffer.

Add the buffer currently shown in WINDOW to the list of WINDOW's previous buffers. WINDOW must be a live window and defaults to the selected one.

If WINDOW is not a minibuffer window, do not record insignificant buffers (buffers whose name starts with a space). If WINDOW is a minibuffer window, record its buffer if and only if that buffer is a live minibuffer (minibufferp with LIVE argument non-nil must return non-nil for it).

Run buffer-list-update-hook if and only if WINDOW is not a minibuffer window.

Aliases

push-window-buffer-onto-prev

Source Code

;; Defined in /usr/src/emacs/lisp/window.el.gz
;;; Windows and buffers.

;; 'prev-buffers' and 'next-buffers' are two reserved window slots used
;; for (1) determining which buffer to show in the window when its
;; buffer shall be buried or killed and (2) which buffer to show for
;; 'switch-to-prev-buffer' and 'switch-to-next-buffer'.

;; 'prev-buffers' consists of <buffer, window-start, window-point>
;; triples.  The entries on this list are ordered by the time their
;; buffer has been removed from the window, the most recently removed
;; buffer's entry being first.  The window-start and window-point
;; components are 'window-start' and 'window-point' at the time the
;; buffer was removed from the window which implies that the entry must
;; be added when 'set-window-buffer' removes the buffer from the window.

;; 'next-buffers' is the list of buffers that have been replaced
;; recently by 'switch-to-prev-buffer'.  These buffers are the least
;; preferred candidates of 'switch-to-prev-buffer' and the preferred
;; candidates of 'switch-to-next-buffer' to switch to.  This list is
;; reset to nil by any action changing the window's buffer with the
;; exception of 'switch-to-prev-buffer' and 'switch-to-next-buffer'.
;; 'switch-to-prev-buffer' pushes the buffer it just replaced on it,
;; 'switch-to-next-buffer' pops the last pushed buffer from it.

;; The following function is called by 'set-window-buffer' _before_ it
;; replaces the buffer of the argument window with the new buffer.  It
;; does not record a non-minibuffer buffer (like the one created by
;; 'calculator' in Electric mode) in a minibuffer window since the code
;; in minibuf.c cannot handle that.  The minibuf.c code calls this
;; function exclusively to arrange minibuffers shown in minibuffer
;; windows.
(defun record-window-buffer (&optional window)
  "Record WINDOW's buffer.
Add the buffer currently shown in WINDOW to the list of WINDOW's
previous buffers.  WINDOW must be a live window and defaults to the
selected one.

If WINDOW is not a minibuffer window, do not record insignificant
buffers (buffers whose name starts with a space).  If WINDOW is a
minibuffer window, record its buffer if and only if that buffer is a
live minibuffer (`minibufferp' with LIVE argument non-nil must return
non-nil for it).

Run `buffer-list-update-hook' if and only if WINDOW is not a minibuffer
window."
  (let* ((window (window-normalize-window window t))
	 (mini (window-minibuffer-p window))
         (buffer (window-buffer window))
         (prev-buffers (window-prev-buffers window))
         (entry (assq buffer prev-buffers)))
    (when entry
      (setq prev-buffers (assq-delete-all buffer prev-buffers)))

    ;; Reset WINDOW's next buffers.  If needed, they are resurrected by
    ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
    (set-window-next-buffers window nil)

    ;; For minibuffer windows record live minibuffers only.  For normal
    ;; windows do not record insignificant buffers.
    (when (if mini
	      (minibufferp buffer t)
	    (not (eq (aref (buffer-name buffer) 0) ?\s)))
      (let ((start (window-start window))
            (point (window-point window)))
	(setq entry
              (cons buffer
                    (with-current-buffer buffer
                      (if entry
                          ;; We have an entry, update marker positions.
                          (list (set-marker (nth 1 entry) start)
				(set-marker (nth 2 entry) point))
			(list (copy-marker start)
                              (copy-marker
                               ;; Preserve window-point-insertion-type
                               ;; (Bug#12855)
                               point window-point-insertion-type))))))
	(set-window-prev-buffers window (cons entry prev-buffers))

	(unless mini
	  (run-hooks 'buffer-list-update-hook))))))