Skip to content

Instantly share code, notes, and snippets.

@jbester
Last active March 11, 2024 14:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbester/c8eb59e65cbe3b177334a171359624d9 to your computer and use it in GitHub Desktop.
Save jbester/c8eb59e65cbe3b177334a171359624d9 to your computer and use it in GitHub Desktop.
Custom Tabline next/prev
(defun custom-tab-line-switch-to-next-tab (&optional mouse-event)
"Switch to the next tab - cycle back to the start at the end.
Its effect is the same as using the `next-buffer' command
(\\[next-buffer]). with the exception it will not open a non-visible buffer"
(interactive (list last-nonmenu-event))
(let ((window (and (listp mouse-event) (posn-window (event-start mouse-event)))))
(with-selected-window (or window (selected-window))
(let* ((tabs (funcall tab-line-tabs-function))
(tab-buffers (mapcar (lambda (tab) (if (bufferp tab) tab (cdr (assq 'buffer tab)))) tabs))
(num-tabs (length tab-buffers))
(tab-position (cl-position (current-buffer) tab-buffers))
(next-tab-position (mod (1+ tab-position) num-tabs))
;; find the buffer
(buffer (nth next-tab-position tab-buffers)))
(when (bufferp buffer)
(if (eq next-tab-position 0)
(progn
(set-window-buffer window buffer)
(set-window-next-buffers window (delq buffer tab-buffers)))
(switch-to-next-buffer window))
)))))
(defun custom-tab-line-switch-to-prev-tab (&optional mouse-event)
"Switch to the prev tab - cycle to the end whe moving past the start
Its effect is the same as using the `prev-buffer' command
(\\[prev-buffer]). with the exception it will not open a non-visible buffer"
(interactive (list last-nonmenu-event))
(let ((window (and (listp mouse-event) (posn-window (event-start mouse-event)))))
(with-selected-window (or window (selected-window))
(let* ((tabs (funcall tab-line-tabs-function))
(tab-buffers (mapcar (lambda (tab) (if (bufferp tab) tab (cdr (assq 'buffer tab)))) tabs))
(num-tabs (length tab-buffers))
(tab-position (cl-position (current-buffer) tab-buffers))
(next-tab-position (mod (1- tab-position) num-tabs))
;; find the buffer
(buffer (nth next-tab-position tab-buffers)))
(when (bufferp buffer)
(if (eq tab-position 0)
(progn
(set-window-buffer window buffer)
(set-window-next-buffers window nil)
;; when clearing the next buffers
;; emacs reuses them as prev buffers
;; but in same order
;; undo the reversal
(set-window-prev-buffers window (reverse (window-prev-buffers window))))
(switch-to-prev-buffer window)) )))))
;; bind the above to keys
(global-set-key (kbd "C-<prior>") #'custom-tab-line-switch-to-prev-tab) ; page up key
(global-set-key (kbd "C-<next>") #'custom-tab-line-switch-to-next-tab) ; page down key
;; rebind tab-line scrolling
(global-set-key [tab-line S-mouse-4] #'custom-tab-line-switch-to-prev-tab)
(global-set-key [tab-line S-mouse-5] #'custom-tab-line-switch-to-next-tab)
(global-set-key [tab-line S-wheel-up] #'custom-tab-line-switch-to-prev-tab)
(global-set-key [tab-line S-wheel-down] #'custom-tab-line-switch-to-next-tab)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment