[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 5a9a017: * lisp/tab-bar.el (tab-bar-switch-to-recent-tab): New co
From: |
Juri Linkov |
Subject: |
master 5a9a017: * lisp/tab-bar.el (tab-bar-switch-to-recent-tab): New command. |
Date: |
Sat, 26 Oct 2019 18:16:18 -0400 (EDT) |
branch: master
commit 5a9a01797b4dad36b81ebfa83eebf96d27a2f395
Author: Juri Linkov <address@hidden>
Commit: Juri Linkov <address@hidden>
* lisp/tab-bar.el (tab-bar-switch-to-recent-tab): New command.
(tab-recent): Alias to tab-bar-switch-to-recent-tab.
(tab-bar--tab-index-recent): New internal function.
(tab-bar-close-tab-select): Add new default option 'recent'.
(tab-bar-close-tab): Handle it.
* lisp/emacs-lisp/seq.el (seq-sort-by, seq-remove): Add autoload.
---
lisp/emacs-lisp/seq.el | 2 ++
lisp/tab-bar.el | 32 ++++++++++++++++++++++++++++----
2 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 8d40930..918b0dc 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -237,6 +237,7 @@ The result is a sequence of the same type as SEQUENCE."
(cl-defmethod seq-sort (pred (list list))
(sort (seq-copy list) pred))
+;;;###autoload
(defun seq-sort-by (function pred sequence)
"Sort SEQUENCE using PRED as a comparison function.
Elements of SEQUENCE are transformed by FUNCTION before being
@@ -295,6 +296,7 @@ list."
exclude))
sequence))))
+;;;###autoload
(cl-defgeneric seq-remove (pred sequence)
"Return a list of all the elements for which (PRED element) is nil in
SEQUENCE."
(seq-filter (lambda (elt) (not (funcall pred elt)))
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index f7b0f26..2b71bf8 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -458,6 +458,16 @@ Return its existing value or a new value."
(seq-position (or tabs (funcall tab-bar-tabs-function))
name (lambda (a b) (equal (cdr (assq 'name a)) b))))
+(defun tab-bar--tab-index-recent (nth &optional tabs)
+ (let* ((tabs (or tabs (funcall tab-bar-tabs-function)))
+ (sorted-tabs
+ (seq-sort-by (lambda (tab) (cdr (assq 'time tab))) #'>
+ (seq-remove (lambda (tab)
+ (eq (car tab) 'current-tab))
+ tabs)))
+ (tab (nth (1- nth) sorted-tabs)))
+ (tab-bar--tab-index tab tabs)))
+
(defun tab-bar-select-tab (&optional arg)
"Switch to the tab by its absolute position ARG in the tab bar.
@@ -514,6 +524,16 @@ to the numeric argument. ARG counts from 1."
(setq arg 1))
(tab-bar-switch-to-next-tab (- arg)))
+(defun tab-bar-switch-to-recent-tab (&optional arg)
+ "Switch to ARGth most recently visited tab."
+ (interactive "p")
+ (unless (integerp arg)
+ (setq arg 1))
+ (let ((tab-index (tab-bar--tab-index-recent arg)))
+ (if tab-index
+ (tab-bar-select-tab (1+ tab-index))
+ (message "No more recent tabs"))))
+
(defun tab-bar-switch-to-tab (name)
"Switch to the tab by NAME."
(interactive (list (completing-read "Switch to tab by name: "
@@ -626,12 +646,14 @@ If ARG is zero, create a new tab in place of the current
tab."
(defvar tab-bar-closed-tabs nil
"A list of closed tabs to be able to undo their closing.")
-(defcustom tab-bar-close-tab-select 'right
+(defcustom tab-bar-close-tab-select 'recent
"Defines what tab to select after closing the specified tab.
If `left', select the adjacent left tab.
-If `right', select the adjacent right tab."
+If `right', select the adjacent right tab.
+If `recent', select the most recently visited tab."
:type '(choice (const :tag "Select left tab" left)
- (const :tag "Select right tab" right))
+ (const :tag "Select right tab" right)
+ (const :tag "Select recent tab" recent))
:group 'tab-bar
:version "27.1")
@@ -682,7 +704,8 @@ TO-INDEX counts from 1."
('left (1- current-index))
('right (if (> (length tabs) (1+ current-index))
(1+ current-index)
- (1- current-index)))))))
+ (1- current-index)))
+ ('recent (tab-bar--tab-index-recent 1 tabs))))))
(setq to-index (max 0 (min (or to-index 0) (1- (length tabs)))))
(tab-bar-select-tab (1+ to-index))
;; Re-read tabs after selecting another tab
@@ -819,6 +842,7 @@ function `tab-bar-tab-name-function'."
(defalias 'tab-select 'tab-bar-select-tab)
(defalias 'tab-next 'tab-bar-switch-to-next-tab)
(defalias 'tab-previous 'tab-bar-switch-to-prev-tab)
+(defalias 'tab-recent 'tab-bar-switch-to-recent-tab)
(defalias 'tab-move 'tab-bar-move-tab)
(defalias 'tab-move-to 'tab-bar-move-tab-to)
(defalias 'tab-rename 'tab-bar-rename-tab)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master 5a9a017: * lisp/tab-bar.el (tab-bar-switch-to-recent-tab): New command.,
Juri Linkov <=