1

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 :)

5
  • The global buffer-list and frame buffer-list cannot entirely be controlled by the user. To have entire control over a list of buffers, it would be necessary to create a different list and manage it separately. Alp Aker uses a unique approach with his frame-bufs library, but that is just an example of how to manage a separate buffer list that can be controlled by the user.
    – lawlist
    Dec 18, 2022 at 17:42
  • In your last example, I think you mean next-buffer x3 - yes?
    – NickD
    Dec 19, 2022 at 5:11
  • @NickD Actually, I meant previous-buffer x4 as it should wrap around. I fixed that with an edit now. Good catch.
    – David
    Dec 19, 2022 at 21:48
  • Wouldn't that depend on how many files are in <more_files_here>?
    – NickD
    Dec 19, 2022 at 22:00
  • @NickD Technically, yes, it would. However, for brevity, I just assumed it is a single file in the example at that point ... but yes, it would be previous-buffer xN + 1 where N = 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.
    – David
    Dec 21, 2022 at 22:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.