]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/tab-line.el (tab-line-move-tab-forward): New command.
authorJuri Linkov <juri@linkov.net>
Tue, 20 May 2025 18:30:38 +0000 (21:30 +0300)
committerEshel Yaron <me@eshelyaron.com>
Wed, 21 May 2025 06:39:25 +0000 (08:39 +0200)
(tab-line-move-tab-backward): New command.
(tab-line-mode-map): Bind 'C-x M-<left>' to
'tab-line-move-tab-backward' and 'C-x M-<right>' to
'tab-line-move-tab-forward'.
(tab-line-switch-repeat-map): Bind 'M-<left>' to
'tab-line-move-tab-backward' and 'M-<right>' to
'tab-line-move-tab-forward'.
Suggested by pinmacs <pinmacs@cas.cat>.

(cherry picked from commit 87fa5f565d70c514bd47b59bb0ef0cba8750e983)

lisp/tab-line.el

index 9d85ade1099872cb8c959ad8a4531ddfa22d3224..d7a9ecf7eb413a2ea108f953a3a4eecdc9e0affa 100644 (file)
@@ -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))))
+
 \f
 (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 <left>"    #'tab-line-switch-to-prev-tab
   "C-x C-<left>"  #'tab-line-switch-to-prev-tab
+  "C-x M-<left>"  #'tab-line-move-tab-backward
   "C-x <right>"   #'tab-line-switch-to-next-tab
-  "C-x C-<right>" #'tab-line-switch-to-next-tab)
+  "C-x C-<right>" #'tab-line-switch-to-next-tab
+  "C-x M-<right>" #'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
-  "<left>"  #'tab-line-switch-to-prev-tab
-  "<right>" #'tab-line-switch-to-next-tab)
+  "<left>"    #'tab-line-switch-to-prev-tab
+  "M-<left>"  #'tab-line-move-tab-backward
+  "<right>"   #'tab-line-switch-to-next-tab
+  "M-<right>" #'tab-line-move-tab-forward)
 
 ;;;###autoload
 (define-minor-mode tab-line-mode