From 3bacf74adb4223e3dded46d63d5743554afd7c94 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 24 Mar 2021 22:14:52 +0200 Subject: [PATCH] * lisp/tab-bar.el (tab-bar-tab-post-change-group-functions): New hook. (tab-bar-change-tab-group): Run it. (tab-bar-move-tab-to-group): New command for new hook. --- etc/NEWS | 4 +++- lisp/tab-bar.el | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 49a4bb8106f..5dbd61ff9ae 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -562,7 +562,9 @@ It also supports a negative argument. *** 'C-x t G' assigns a group name to the tab. 'tab-close-group' can close all tabs that belong to the selected group. The user option 'tab-bar-new-tab-group' defines the default group of a -new tab. +new tab. After customizing 'tab-bar-tab-post-change-group-functions' +to 'tab-bar-move-tab-to-group', changing the tab group will also move it +closer to other tabs in the same group. --- *** New user option 'tab-bar-tab-name-format-function'. diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 63769673b93..2e27b293c5e 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -1412,6 +1412,42 @@ function `tab-bar-tab-name-function'." ;;; Tab groups +(defun tab-bar-move-tab-to-group (&optional tab) + "Relocate TAB (or the current tab) closer to its group." + (interactive) + (let* ((tabs (funcall tab-bar-tabs-function)) + (tab (or tab (tab-bar--current-tab-find tabs))) + (tab-index (tab-bar--tab-index tab)) + (group (alist-get 'group tab)) + ;; Beginning position of the same group + (beg (seq-position tabs group + (lambda (tb gr) + (and (not (eq tb tab)) + (equal (alist-get 'group tb) gr))))) + ;; Size of the same group + (len (when beg + (seq-position (nthcdr beg tabs) group + (lambda (tb gr) + (not (equal (alist-get 'group tb) gr)))))) + (pos (when beg + (cond + ;; Don't move tab when it's already inside group bounds + ((and len (>= tab-index beg) (<= tab-index (+ beg len))) nil) + ;; Move tab from the right to the group end + ((and len (> tab-index (+ beg len))) (+ beg len 1)) + ;; Move tab from the left to the group beginning + ((< tab-index beg) beg))))) + (when pos + (tab-bar-move-tab-to pos (1+ tab-index))))) + +(defcustom tab-bar-tab-post-change-group-functions nil + "List of functions to call after changing a tab group. +The current tab is supplied as an argument." + :type 'hook + :options '(tab-bar-move-tab-to-group) + :group 'tab-bar + :version "28.1") + (defun tab-bar-change-tab-group (group-name &optional arg) "Add the tab specified by its absolute position ARG to GROUP-NAME. If no ARG is specified, then set the GROUP-NAME for the current tab. @@ -1445,6 +1481,8 @@ While using this command, you might also want to replace (setcdr group group-new-name) (nconc tab `((group . ,group-new-name)))) + (run-hook-with-args 'tab-bar-tab-post-change-group-functions tab) + (force-mode-line-update) (unless tab-bar-mode (message "Set tab group to '%s'" group-new-name)))) -- 2.39.2