Switch corresponding tab when switching between buffers?
Yevgnen opened this issue · comments
Hi, we can now filter buffers under specific tab. Taking a different approach, can we also have an option to switch corresponding tab automatically when switching between buffers?
Since there is no guarantee that a buffer will be in only one tabspace this would be somewhat tricky to implement. I think a better option would be to use something like embark to act on a buffer from the full (i.e. across all tabs) buffer list. Of course, I'm happy to accept a pull request if you end up implementing this.
Since there is no guarantee that a buffer will be in only one tabspace this would be somewhat tricky to implement.
For my cases, most of the time the behavior would be ideal. For the cases where a buffer is in more than one tabspace, you could simply prompt the user with the list of tabspaces?
Of course, this would almost certainly need to be opt-in since I'm sure many others workflows have lots of duplication of buffers across tabspaces.
Yeah, making it opt in seems like a good idea. I'm not sure I have time right now to implement something like this but it seems a fair request. If I can find time soon I will try and see what i can do. As I said above though I welcome PRs!
@Yevgnen and @ParetoOptimalDev I've added a function tabspaces-switch-buffer-and-tab
which I think provides the desired behavior (see f48b041). It allows you to see the full buffer list (not just the ones in the current tabspace) and switch to the buffer in its tab. If that buffer does not exist you have the option of either opening a new tabspace with the buffer or opening the buffer in the current tabspace. If you would test it out and let me know what you think that would be very helpful.
See f25bb67 for revisions and coverage of the case in which the buffer is open in more than one tabspace. I'm sure the code can be improved so suggestions welcome.
@mclearc Wow thanks! I'll try this out today 😀
Hi @mclearc, I just got around to trying this. It enables half of what I need to make my "project buffers by default, but when 'narrowing' with b to show all buffers switch tabspaces` vision.
I think that with this new function I should be able to get the functionality by just adding this to my config?
(plist-put consult--source-buffer :action #'tabspaces-switch-buffer-and-tab)
Edit: That apparently asks me to Select tab
and provides no completions.
OH! It's because I'm using previews in consult which implicitly opens the buffer in the tabspace.
That makes me realize that switching tab on preview might would improve this idea even more!
But that would require making consult-register-format
tabspace aware if I understand correctly.
Even with preview disabled it seems it doesn't work and falls into the "create buffer in new tab" case.
If you use embark I think you could run an embark action on the buffer from the full buffer list?
Yeah so something like the following works for me.
(defvar-keymap embark-tabspaces-actions
:doc "Keymap for actions for tabspaces."
:parent embark-general-map
"B" #'tab-bar-select-tab-by-name)
(add-to-list 'embark-keymap-alist '(buffer . embark-tabspaces-actions))
So I can open consult-buffer, this has my tabspace buffers at the top. I can then press "b" to show all buffers in all tabspaces. Narrow to the one I want. Run embark-act
on it. And then choose "B" to switch to the buffer and tab. This seems elegant enough for me, so I'm unlikely to build it into tabspaces itself.
@mclearc I understand your position, but I just had a realization about the nature of what I'm asking for.
Right now when you narrow with b
in consult-buffer to show all buffers the default action is to "add selected buffer to your current workspace".
I want the default behavior when narrowed to b
in consult-buffer to be "switch to tab buffer is opened in" because it's most common for me to only show all buffers when I need to switch to a different tabspace.
Then I'd add an embark action for the less common case (for me) for "add selected buffer to your current workspace".
I'm guessing your common case when showing all buffers is to add a buffer to the tabspace and this won't affect your current position?
Would it be amenable to provide customization for something like "consult-all-buffers-default-action-function"? I'm not quite sure if that's possible or would add to much complexity. I also don't know if there are others whose common case matches mine or I'm the odd one out 🙃
Anyway, thanks for the snippet, discussion, etc! 😄
So this ends up being about configuring consult, and not tabspaces. But here is the kind of thing I think you want?
(defvar consult--source-tab-buffer
`(:name "Tab & Buffer"
:narrow ?B
:hidden t
:category buffer
:face consult-buffer
:history buffer-name-history
:items
,(lambda () (consult--buffer-query :sort 'visibility
:as #'buffer-name))
:action ,#'tabspaces-switch-buffer-and-tab)
"Buffer candidate source for `consult-buffer'.")
(add-to-list 'consult-buffer-sources consult--source-tab-buffer)
So narrow using "B" and you get a list of all open buffers. Selecting a buffer with "Return" will take you to that buffer in its tab.
So this ends up being about configuring consult, and not tabspaces. But here is the kind of thing I think you want?
(defvar consult--source-tab-buffer `(:name "Tab & Buffer" :narrow ?B :hidden t :category buffer :face consult-buffer :history buffer-name-history :items ,(lambda () (consult--buffer-query :sort 'visibility :as #'buffer-name)) :action ,#'tabspaces-switch-buffer-and-tab) "Buffer candidate source for `consult-buffer'.") (add-to-list 'consult-buffer-sources consult--source-tab-buffer)So narrow using "B" and you get a list of all open buffers. Selecting a buffer with "Return" will take you to that buffer in its tab.
Wow...That is perfect! Thanks for the support.