How to switch buffers (prev & next) with minimal buffer list order modification?
To be succinct and without getting into the weeds of particular details, the best way to explain what I am trying to do is through a simple example. Given a list of already open buffers, I want to have the following results for buffer-list arrangement as I navigate by previous-buffer
and/or next-buffer
.
Initial buffer list (* = currently displayed buffer)
HelloWorld.txt
* App.js
init.el
ComplexObjectFactory.java
some-elisp.el
AnotherFile.txt
<more_files_here>
When I hit my keybind for previous-buffer
(or whatever custom function needs to be bound instead):
HelloWorld.txt
App.js
* init.el
ComplexObjectFactory.java
some-elisp.el
AnotherFile.txt
<more_files_here>
When I hit my keybind for next-buffer
:
HelloWorld.txt
* App.js
init.el
ComplexObjectFactory.java
some-elisp.el
AnotherFile.txt
<more_files_here>
When I hit my keybind for previous-buffer
x4:
HelloWorld.txt
App.js
init.el
ComplexObjectFactory.java
some-elisp.el
* AnotherFile.txt
<more_files_here>
When I hit my keybind for next-buffer
x2:
HelloWorld.txt
App.js
init.el
* ComplexObjectFactory.java
some-elisp.el
AnotherFile.txt
<more_files_here>
When I hit my keybind for previous-buffer
x4:
* HelloWorld.txt
App.js
init.el
ComplexObjectFactory.java
some-elisp.el
AnotherFile.txt
<more_files_here>
When I switch buffers via C-x b, the selected buffer is inserted into current position of the current buffer and the current buffer becomes the previous the buffer (the rest are shifted back/down) but all of the "next buffers" stay the same. When I open a new file as a buffer, the same type of behavior happens.
Essentially, the buffer list order is maintained with respect to the originally opened order of buffers and only changed by adding more recently opened/selected buffers as the previous buffers of the current buffer but leaving the next buffers untouched (except when one of them would now become a previous buffer).
If that makes any sense. How would I go about getting this kind of functionality/workflow? I have looked at the previous-buffer
and next-buffer
functions inside of window.el
but the issue is that the logic for switch-to-prev-buffer
and switch-to-next-buffer
is so unimaginably complex for what I would expect to be a simple piece of functionality. It appears that everything is so deeply intertwined for buffers, windows, and files that I honestly don't know how to untangle the spaghetti. I would have imagined this might even be a configurable setting but no luck finding anything. Any help is appreciated :)
next-buffer x3
- yes?previous-buffer x4
as it should wrap around. I fixed that with an edit now. Good catch.<more_files_here>
?previous-buffer xN + 1
whereN = the number of buffers represented by "<more_files_here>"
for the proper wrap around formula. Does that help clarify things? Sorry about that over simplification.