akermu / cbm.el

Cycle by major - Cycle through buffers with the same major-mode
10 stars 4 forks source link

cycle backwards #3

Open mihaiolteanu opened 2 years ago

mihaiolteanu commented 2 years ago

How would one cycle backwards using cbm?

That is, suppose I have three buffers, one, two, three. The current selected buffer is one. I call cbm-cycle and now the selected buffer is two. If I want to select the buffer one again, I would have to call cbm-cycle two times, one to select buffer three and the second time to wrap-around to buffer one. How would one implement a cbm-cycle-back function that would go from two to one in the previous scenario in a single step?

letlambdafree commented 2 years ago

You can use this function.

(defun cbm-cycle-reverse ()
    "Cycles reverse through buffers with same `major-mode'."
    (interactive)
    (unless (eq last-command #'cbm-cycle-reverse)
      (setq cbm-buffers nil))
    (unless cbm-buffers
      (cbm-make-buffer-list))
    (setq cbm-buffers (append (last cbm-buffers) (butlast cbm-buffers)))
    (let ((buffer (car cbm-buffers)))
      (when (bufferp buffer)
        (switch-to-buffer buffer))))

I tested this with 5 buffers, it worked. (note: last and butlast are in ‘subr.el’.)