summaryrefslogtreecommitdiff
path: root/.emacs.d/site-lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2022-01-29 16:11:29 -0700
committerSean Whitton <spwhitton@spwhitton.name>2022-01-29 16:58:47 -0700
commitbdd370317bee322958e2f2570c46cff76afd7d65 (patch)
tree2aae83fd14a4218cff8c591d8376c1369a237dce /.emacs.d/site-lisp
parenta863a322b8b5eb1dd18948fde8be5f7e2ca4f13b (diff)
downloaddotfiles-bdd370317bee322958e2f2570c46cff76afd7d65.tar.gz
attempt to extract transient-cycles-tab-bar-mode
Diffstat (limited to '.emacs.d/site-lisp')
-rw-r--r--.emacs.d/site-lisp/transient-cycles.el72
1 files changed, 72 insertions, 0 deletions
diff --git a/.emacs.d/site-lisp/transient-cycles.el b/.emacs.d/site-lisp/transient-cycles.el
index f03df062..d51b4125 100644
--- a/.emacs.d/site-lisp/transient-cycles.el
+++ b/.emacs.d/site-lisp/transient-cycles.el
@@ -474,6 +474,78 @@ since then. Otherwise, call `previous-buffer'."
(setq param (* -1 count))))
(t (previous-buffer)))))
+(defvar transient-cycles-tab-bar-mode-map (make-sparse-keymap)
+ "Keymap for `transient-cycles-tab-bar-mode'.")
+
+;;;###autoload
+(define-minor-mode transient-cycles-tab-bar-mode
+ "Augment \\[tab-previous], \\[tab-next] and
+\\[tab-bar-switch-to-recent-tab] with transient cycling. After
+running those commands, you can use
+`transient-cycles-tab-bar-cycle-backwards-key' and
+`transient-cycles-tab-bar-cycle-forwards-key' to move forwards
+and backwards in the list of tabs. When transient cycling
+completes, tab access times will be as though you had moved
+directly from the first tab to the final tab."
+ :lighter nil :keymap transient-cycles-tab-bar-mode-map :global t
+ :group 'transient-cycles)
+
+(defcustom transient-cycles-tab-bar-cycle-backwards-key [left]
+ "Key to cycle backwards in the transient maps set by commands
+augmented by `transient-cycles-tab-bar-mode'."
+ :type 'key-sequence
+ :group 'transient-cycles)
+
+(defcustom transient-cycles-tab-bar-cycle-forwards-key [right]
+ "Key to cycle forwards in the transient maps set by commands
+augmented by `transient-cycles-tab-bar-mode'."
+ :type 'key-sequence
+ :group 'transient-cycles)
+
+(transient-cycles-define-commands (recent-tab-old-time)
+ (([remap tab-previous] (count)
+ (setq recent-tab-old-time (transient-cycles--nth-tab-time (* -1 count)))
+ (tab-previous count))
+
+ ;; `tab-bar-switch-to-recent-tab' does not have a binding by default but
+ ;; establish a remapping so that the user can easily access the transient
+ ;; cycling variant simply by adding a binding for the original command.
+ ([remap tab-bar-switch-to-recent-tab] (count)
+ (setq recent-tab-old-time
+ (cl-loop for tab in (funcall tab-bar-tabs-function)
+ unless (eq 'current-tab (car tab))
+ minimize (cdr (assq 'time tab))))
+ (tab-bar-switch-to-recent-tab count))
+
+ ([remap tab-next] (count)
+ (setq recent-tab-old-time (transient-cycles--nth-tab-time count))
+ (tab-next count)))
+
+ (lambda (_ignore)
+ (lambda (count)
+ ;; We are moving away from the current tab, so restore its time as if
+ ;; we had never selected it, and store the time of the tab we're
+ ;; moving to in case we need to do this again.
+ (let ((next-tab-old-time (transient-cycles--nth-tab-time count)))
+ (tab-bar-switch-to-next-tab count)
+ (cl-loop with max
+ for tab in (funcall tab-bar-tabs-function)
+ for tab-time = (assq 'time tab)
+ when (and (not (eq 'current-tab (car tab)))
+ (or (not max) (> (cdr tab-time) (cdr max))))
+ do (setq max tab-time)
+ finally (setcdr max recent-tab-old-time))
+ (setq recent-tab-old-time next-tab-old-time))))
+ :keymap transient-cycles-tab-bar-mode-map
+ :cycle-forwards-key transient-cycles-tab-bar-cycle-forwards-key
+ :cycle-backwards-key transient-cycles-tab-bar-cycle-backwards-key)
+
+(defun transient-cycles--nth-tab-time (n)
+ (let* ((tabs (funcall tab-bar-tabs-function))
+ (current-index (cl-position 'current-tab tabs :key #'car))
+ (new-index (mod (+ n current-index) (length tabs))))
+ (alist-get 'time (nth new-index tabs))))
+
(provide 'transient-cycles)
;;; transient-cycles.el ends here