TL;DR: Any suggestions as to what function(s) can help me restore (to any given window) the buffer that was visible most recently? It's important that the currently visible buffer is not killed, just moved to the back.
This is partly an exercise in Elisp for me so my preference is for building such a function myself instead of using off-the-shelf packages.
The long version:
I'm trying to create a function which effectively "throws" a newly created buffer to the other window. I've captured some but not all of the necessary functionality.
An example:
- Let's say that my current frame is split vertically into two windows,
|Window 1|Window 2|
; I'm editingfoo.el
inWindow 1
andsome-junk.el
inWindow 2
. - Now, let's say that I open a file
bar.el
which I want to see alongsidefoo.el
(viaido
,helm
, etc.). - However, I accidentally opened
bar.el
in inWindow 1
, thereby covering upfoo.el
. - (I find myself in similar non-accidental situations, hence the motivation for such a function)
The goal is to have a function which will "throw" bar.el
to Window 2
, i.e., two steps: (i) have Window 2
show bar.el
and (ii) have Window 1
show foo.el
.
So far, I've been able to come up with an implementation of (i) but am stuck on (ii). in other words, I can get Window 2
to show bar.el
but I'm having trouble getting Window 1
to revert to foo.el
:
(defun dc/move-top-buffer-to-other-window ()
(interactive)
(let* ((current (selected-window))
(other (next-window))
(current-buffer (window-buffer current)))
(set-window-buffer other current-buffer)
(other-window 1)))
I thought there might be a variable that represents a notion of "buffer-order" for each window (e.g., in the example 1, such a variable for Window 2
would hold a list of buffers in the following order, as per the example above, [bar.el
foo.el
...]), since Emacs is able to "remember" which buffer I had open last. But I haven't been able to find it.
Any suggestions?