From a69743eb9376b8f6fc04ef14048a310f9387e733 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Tue, 20 May 2025 21:30:38 +0300 Subject: [PATCH] * lisp/tab-line.el (tab-line-move-tab-forward): New command. (tab-line-move-tab-backward): New command. (tab-line-mode-map): Bind 'C-x M-' to 'tab-line-move-tab-backward' and 'C-x M-' to 'tab-line-move-tab-forward'. (tab-line-switch-repeat-map): Bind 'M-' to 'tab-line-move-tab-backward' and 'M-' to 'tab-line-move-tab-forward'. Suggested by pinmacs . (cherry picked from commit 87fa5f565d70c514bd47b59bb0ef0cba8750e983) --- lisp/tab-line.el | 52 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lisp/tab-line.el b/lisp/tab-line.el index 9d85ade1099..d7a9ecf7eb4 100644 --- a/lisp/tab-line.el +++ b/lisp/tab-line.el @@ -1003,7 +1003,7 @@ is possible when `tab-line-switch-cycling' is non-nil." (switch-to-buffer buffer)))))))) (defun tab-line-mouse-move-tab (event) - "Move a tab to a different position on the tab line. + "Move a tab to a different position on the tab line using mouse. This command should be bound to a drag event. It moves the tab at the mouse-down event to the position at mouse-up event. It can be used only when `tab-line-tabs-function' is @@ -1027,6 +1027,46 @@ customized to `tab-line-tabs-fixed-window-buffers'." (set-window-parameter window1 'tab-line-cache nil) (with-selected-window window1 (force-mode-line-update)))))) +(defun tab-line-move-tab-forward (&optional arg) + "Move a tab to a different position on the tab line. +ARG specifies the number of positions to move: +- When positive, move the current tab ARG positions to the right. +- When negative, move the current tab -ARG positions to the left. +- When nil, act as if ARG is 1, moving one position to the right. +It can be used only when `tab-line-tabs-function' is +customized to `tab-line-tabs-fixed-window-buffers'." + (interactive "p") + (when (eq tab-line-tabs-function #'tab-line-tabs-fixed-window-buffers) + (let* ((window (selected-window)) + (buffers (window-parameter window 'tab-line-buffers)) + (buffer (current-buffer)) + (pos (seq-position buffers buffer)) + (len (length buffers)) + (new-pos (+ pos (or arg 1)))) + (when (and pos (> len 1)) + (setq new-pos (if tab-line-switch-cycling + (mod new-pos len) + (max 0 (min new-pos (1- len))))) + (setq buffers (delq buffer buffers)) + (setq buffers (append + (seq-take buffers new-pos) + (list buffer) + (seq-drop buffers new-pos))) + (set-window-parameter window 'tab-line-buffers buffers) + (set-window-parameter window 'tab-line-cache nil) + (force-mode-line-update))))) + +(defun tab-line-move-tab-backward (&optional arg) + "Move a tab to a different position on the tab line. +ARG specifies the number of positions to move: +- When positive, move the current tab ARG positions to the left. +- When negative, move the current tab -ARG positions to the right. +- When nil, act as if ARG is 1, moving one position to the left. +It can be used only when `tab-line-tabs-function' is +customized to `tab-line-tabs-fixed-window-buffers'." + (interactive "p") + (tab-line-move-tab-forward (- (or arg 1)))) + (defcustom tab-line-close-tab-function 'bury-buffer "What to do upon closing a tab on the tab line. @@ -1132,14 +1172,18 @@ However, return the correct mouse position list if EVENT is a :doc "Keymap for keys of `tab-line-mode'." "C-x " #'tab-line-switch-to-prev-tab "C-x C-" #'tab-line-switch-to-prev-tab + "C-x M-" #'tab-line-move-tab-backward "C-x " #'tab-line-switch-to-next-tab - "C-x C-" #'tab-line-switch-to-next-tab) + "C-x C-" #'tab-line-switch-to-next-tab + "C-x M-" #'tab-line-move-tab-forward) (defvar-keymap tab-line-switch-repeat-map :doc "Keymap to repeat tab/buffer cycling. Used in `repeat-mode'." :repeat t - "" #'tab-line-switch-to-prev-tab - "" #'tab-line-switch-to-next-tab) + "" #'tab-line-switch-to-prev-tab + "M-" #'tab-line-move-tab-backward + "" #'tab-line-switch-to-next-tab + "M-" #'tab-line-move-tab-forward) ;;;###autoload (define-minor-mode tab-line-mode -- 2.39.5