Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Perspective-specific next/prev buffer switch #63

Open
Ailrun opened this issue Dec 20, 2016 · 14 comments
Open

Feature Request: Perspective-specific next/prev buffer switch #63

Ailrun opened this issue Dec 20, 2016 · 14 comments

Comments

@Ailrun
Copy link

Ailrun commented Dec 20, 2016

At least I know, perspective doesn't have features that is like switch-to-next-buffer or switch-to-prev-buffer using specific perspective.

In detail, there is no simple way to change C-x <C-left> (switch-to-prev-buffer) to perspective-relative one.
For example, suppose that I have 2 perspective perspA and perspB, and there are 5 buffers in perspA, and there are 7 buffers in perspB. In this case, I can't traverse my buffers only which are in perspA, and while traversing, all buffers of perspB will join to perspA and everything is messed up.

Could you add these features(next/prev) to this wonderful package?

@inigoserna
Copy link

inigoserna commented Aug 15, 2017

I use these functions to achieve the same behaviour you are asking for:

(defun isr/persp-get-buffers-list ()
  "Get filtered list of buffers, sorted alphabetically."
  (sort
   (cl-remove-if '(lambda (b) (if (stringp b) (string-match "^\[* \]" b) t))
                 (mapcar 'buffer-name (persp-buffers persp-curr)))
   'string<))

(defun isr/persp-next-buffer ()
  "My own version of `next-buffer'. Don't show internal buffers."
  (interactive)
  (let ((buffername (buffer-name (current-buffer)))
        (bufferlist (isr/persp-get-buffers-list)))
    (switch-to-buffer (or (cadr (member buffername bufferlist)) (car bufferlist)))))

(defun isr/persp-previous-buffer ()
  "My own version of `next-buffer'. Don't show internal buffers"
  (interactive)
  (let ((buffername (buffer-name (current-buffer)))
        (bufferlist (reverse (isr/persp-get-buffers-list))))
    (switch-to-buffer (or (cadr (member buffername bufferlist)) (car bufferlist)))))

@Ailrun
Copy link
Author

Ailrun commented Aug 21, 2017

@inigoserna
Thx for your info! I will try it!
Why don't you send a PR?

@inigoserna
Copy link

IMO it's a personal customization, not something everyone could be interested in.

@Ailrun
Copy link
Author

Ailrun commented Aug 26, 2017

But it's useful to have, isn't it?

@inigoserna
Copy link

Well, I use it daily ;)

@Ailrun
Copy link
Author

Ailrun commented Aug 27, 2017

@inigoserna I mean, it's useful, so there's worth to send a PR with this feature, isn't it?

@tonycpsu
Copy link

Looking for something like this myself, and the functions above seem to not be compatible with current versions of perspective. I'm getting Symbol’s value as variable is void: persp-curr.

@gcv
Copy link
Collaborator

gcv commented Jan 28, 2020

A few thoughts on this.

To address @tonycpsu's comment about @inigoserna's code not working with current Perspective. You can fix it by changing persp-curr to (persp-curr). If that's the functionality you want, that should be enough.

However, I don't think that code really does the right thing with regard to going to the "next" or "previous" buffer. Since it sorts buffers alphabetically, it actually goes to the next or previous buffer in alphabetical order, not in chronological last-viewed order as the Emacs built-in switch-to-next-buffer and switch-to-previous-buffer do.

Writing an implementation of persp-next-buffer and persp-previous-buffer which does the right thing with regard to chronological sorting is tricky. The equivalent Emacs built-ins are quite complex (and IMO have bad behavior with regard to uninteresting and temporary buffers anyway), and I don't see an easy way to make them respect Perspective.

I recently added Perspective-aware implementations of several buffer switchers, in addition to the Ido support which has been available for a long time. See the updated README for details. Would those work instead of a mechanism for iterating through all of a perspective's buffers in a given window?

@gcv
Copy link
Collaborator

gcv commented Feb 13, 2020

Closing this unless someone convinces me that Perspective-aware buffer switchers are inadequate, and that there's a reasonably simple implementation path.

@titibandit
Copy link
Contributor

I don't know if this is right, as this seems almost too easy, but I think I've achieved the desired behaviour using

  (defun +titi/persp-skip-buffers (window buff bury-or-kill)
    (not (persp-is-current-buffer buff)))
  (setq switch-to-prev-buffer-skip '+titi/persp-skip-buffers)

basically just forbidding the [previous|next]-buffer functions to switch to buffers that are not part of the current perspective. Doing this preserves the chronological order of last viewed buffers.
This conversation maybe predates the switch-to-prev-buffer-skip variable.

@Ailrun
Copy link
Author

Ailrun commented Oct 29, 2024

@titibandit Yeah, if I know correctly, it is introduced 27.1 or so, which comes after this issue. But anyway, thank you for providing the snippet!

@titibandit
Copy link
Contributor

@Ailrun You're welcome. And yes, it's been introduced in 27.1. I've made a pull request that mentions this customization in the readme file #211

@gcv
Copy link
Collaborator

gcv commented Oct 30, 2024

Very nice! Thank you @titibandit.

I’m open to automatically setting switch-to-prev-buffer-skip when Perspective is activated on Emacs 27+, but it’s mildly annoying to do in a way that respects the user’s existing settings of switch-to-prev-buffer-skip. (I think the right way is to add a “customize” setting to opt out of setting switch-to-prev-buffer-skip, then save the user’s existing setting when persp-mode is turned on, and restore it when persp-mode is turned off.)

@titibandit
Copy link
Contributor

Oh I thought since this is a user setting, it should be, from the perspective (pun not intended) of this package, left alone and only hinted at in the Readme. But it's true that this behavior makes just so much more sense. Having it set automatically by persp-mode is a real improvement to the package I think.

@gcv gcv reopened this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants