diff options
| author | Juri Linkov | 2024-04-16 09:40:15 +0300 |
|---|---|---|
| committer | Juri Linkov | 2024-04-16 09:40:15 +0300 |
| commit | cdd37ba4e853dcb31d8a85e12526b509720b37cd (patch) | |
| tree | a05c605687d75f0c57e63038ea20b86d3a8d9de4 | |
| parent | b33fb3b69cb9d4bce3a8cd12771649b3c3945fb0 (diff) | |
| download | emacs-cdd37ba4e853dcb31d8a85e12526b509720b37cd.tar.gz emacs-cdd37ba4e853dcb31d8a85e12526b509720b37cd.zip | |
Support prefix argument for switching tabs in tab-line-mode
* lisp/tab-line.el (tab-line-select-tab-buffer): Optimize.
(tab-line-switch-cycling): Enable by default like in tab-bar-mode.
(tab-line-switch-to-prev-tab, tab-line-switch-to-next-tab):
Add a prefix argument ARG and support it for switching tabs.
Improve docstring.
| -rw-r--r-- | lisp/tab-line.el | 113 |
1 files changed, 63 insertions, 50 deletions
diff --git a/lisp/tab-line.el b/lisp/tab-line.el index 54e9ee16243..48272b7b4b3 100644 --- a/lisp/tab-line.el +++ b/lisp/tab-line.el | |||
| @@ -843,29 +843,27 @@ using the `previous-buffer' command." | |||
| 843 | (force-mode-line-update)))))))) | 843 | (force-mode-line-update)))))))) |
| 844 | 844 | ||
| 845 | (defun tab-line-select-tab-buffer (buffer &optional window) | 845 | (defun tab-line-select-tab-buffer (buffer &optional window) |
| 846 | (let* ((window-buffer (window-buffer window)) | 846 | (if (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) |
| 847 | (next-buffers (seq-remove (lambda (b) (eq b window-buffer)) | 847 | (let* ((window-buffer (window-buffer window)) |
| 848 | (window-next-buffers window))) | 848 | (next-buffers (seq-remove (lambda (b) (eq b window-buffer)) |
| 849 | (prev-buffers (seq-remove (lambda (b) (eq b window-buffer)) | 849 | (window-next-buffers window))) |
| 850 | (mapcar #'car (window-prev-buffers window)))) | 850 | (prev-buffers (seq-remove (lambda (b) (eq b window-buffer)) |
| 851 | ;; Remove next-buffers from prev-buffers | 851 | (mapcar #'car (window-prev-buffers window)))) |
| 852 | (prev-buffers (seq-difference prev-buffers next-buffers))) | 852 | ;; Remove next-buffers from prev-buffers |
| 853 | (cond | 853 | (prev-buffers (seq-difference prev-buffers next-buffers))) |
| 854 | ((and (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) | 854 | (cond |
| 855 | (memq buffer next-buffers)) | 855 | ((memq buffer next-buffers) |
| 856 | (dotimes (_ (1+ (seq-position next-buffers buffer))) | 856 | (dotimes (_ (1+ (seq-position next-buffers buffer))) |
| 857 | (switch-to-next-buffer window))) | 857 | (switch-to-next-buffer window))) |
| 858 | ((and (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) | 858 | ((memq buffer prev-buffers) |
| 859 | (memq buffer prev-buffers)) | 859 | (dotimes (_ (1+ (seq-position prev-buffers buffer))) |
| 860 | (dotimes (_ (1+ (seq-position prev-buffers buffer))) | 860 | (switch-to-prev-buffer window))))) |
| 861 | (switch-to-prev-buffer window))) | 861 | (with-selected-window window |
| 862 | (t | 862 | (let ((switch-to-buffer-obey-display-actions nil)) |
| 863 | (with-selected-window window | 863 | (switch-to-buffer buffer))))) |
| 864 | (let ((switch-to-buffer-obey-display-actions nil)) | 864 | |
| 865 | (switch-to-buffer buffer))))))) | 865 | (defcustom tab-line-switch-cycling t |
| 866 | 866 | "Wrap tabs on tab switch while cycling. | |
| 867 | (defcustom tab-line-switch-cycling nil | ||
| 868 | "Enable cycling tab switch. | ||
| 869 | If non-nil, `tab-line-switch-to-prev-tab' in the first tab | 867 | If non-nil, `tab-line-switch-to-prev-tab' in the first tab |
| 870 | switches to the last tab and `tab-line-switch-to-next-tab' in the | 868 | switches to the last tab and `tab-line-switch-to-next-tab' in the |
| 871 | last tab switches to the first tab. This variable is not consulted | 869 | last tab switches to the first tab. This variable is not consulted |
| @@ -874,47 +872,62 @@ when `tab-line-tabs-function' is `tab-line-tabs-window-buffers'." | |||
| 874 | :group 'tab-line | 872 | :group 'tab-line |
| 875 | :version "28.1") | 873 | :version "28.1") |
| 876 | 874 | ||
| 877 | (defun tab-line-switch-to-prev-tab (&optional event) | 875 | (defun tab-line-switch-to-prev-tab (&optional event arg) |
| 878 | "Switch to the previous tab's buffer. | 876 | "Switch to the ARGth previous tab's buffer. |
| 879 | Its effect is the same as using the `previous-buffer' command | 877 | When `tab-line-tabs-function' is `tab-line-tabs-window-buffers', |
| 880 | (\\[previous-buffer])." | 878 | its effect is the same as using the `previous-buffer' command |
| 881 | (interactive (list last-nonmenu-event)) | 879 | \(\\[previous-buffer]). |
| 880 | For other values of `tab-line-tabs-function' this command | ||
| 881 | switches to the previous buffer in the sequence defined by | ||
| 882 | `tab-line-tabs-function'. To wrap buffer cycling in this case | ||
| 883 | is possible when `tab-line-switch-cycling' is non-nil." | ||
| 884 | (interactive (list last-nonmenu-event | ||
| 885 | (prefix-numeric-value current-prefix-arg))) | ||
| 882 | (let ((window (and (listp event) (posn-window (event-start event))))) | 886 | (let ((window (and (listp event) (posn-window (event-start event))))) |
| 883 | (if (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) | 887 | (with-selected-window (or window (selected-window)) |
| 884 | (switch-to-prev-buffer window) | 888 | (if (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) |
| 885 | (with-selected-window (or window (selected-window)) | 889 | (previous-buffer arg t) |
| 886 | (let* ((buffers (seq-keep | 890 | (let* ((buffers (seq-keep |
| 887 | (lambda (tab) (or (and (bufferp tab) tab) | 891 | (lambda (tab) (or (and (bufferp tab) tab) |
| 888 | (alist-get 'buffer tab))) | 892 | (alist-get 'buffer tab))) |
| 889 | (funcall tab-line-tabs-function))) | 893 | (funcall tab-line-tabs-function))) |
| 890 | (pos (seq-position buffers (current-buffer))) | 894 | (old-pos (seq-position buffers (current-buffer))) |
| 891 | (buffer (when pos | 895 | (new-pos (when old-pos (- old-pos (or arg 1)))) |
| 892 | (if (and tab-line-switch-cycling (<= pos 0)) | 896 | (new-pos (when new-pos |
| 893 | (nth (1- (length buffers)) buffers) | 897 | (if tab-line-switch-cycling |
| 894 | (nth (1- pos) buffers))))) | 898 | (mod new-pos (length buffers)) |
| 899 | (max new-pos 0)))) | ||
| 900 | (buffer (when new-pos (nth new-pos buffers)))) | ||
| 895 | (when (bufferp buffer) | 901 | (when (bufferp buffer) |
| 896 | (let ((switch-to-buffer-obey-display-actions nil)) | 902 | (let ((switch-to-buffer-obey-display-actions nil)) |
| 897 | (switch-to-buffer buffer)))))))) | 903 | (switch-to-buffer buffer)))))))) |
| 898 | 904 | ||
| 899 | (defun tab-line-switch-to-next-tab (&optional event) | 905 | (defun tab-line-switch-to-next-tab (&optional event arg) |
| 900 | "Switch to the next tab's buffer. | 906 | "Switch to the next ARGth tab's buffer. |
| 901 | Its effect is the same as using the `next-buffer' command | 907 | When `tab-line-tabs-function' is `tab-line-tabs-window-buffers', |
| 902 | (\\[next-buffer])." | 908 | its effect is the same as using the `next-buffer' command |
| 903 | (interactive (list last-nonmenu-event)) | 909 | \(\\[next-buffer]). |
| 910 | For other values of `tab-line-tabs-function' this command | ||
| 911 | switches to the next buffer in the sequence defined by | ||
| 912 | `tab-line-tabs-function'. To wrap buffer cycling in this case | ||
| 913 | is possible when `tab-line-switch-cycling' is non-nil." | ||
| 914 | (interactive (list last-nonmenu-event | ||
| 915 | (prefix-numeric-value current-prefix-arg))) | ||
| 904 | (let ((window (and (listp event) (posn-window (event-start event))))) | 916 | (let ((window (and (listp event) (posn-window (event-start event))))) |
| 905 | (if (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) | 917 | (with-selected-window (or window (selected-window)) |
| 906 | (switch-to-next-buffer window) | 918 | (if (eq tab-line-tabs-function #'tab-line-tabs-window-buffers) |
| 907 | (with-selected-window (or window (selected-window)) | 919 | (next-buffer arg t) |
| 908 | (let* ((buffers (seq-keep | 920 | (let* ((buffers (seq-keep |
| 909 | (lambda (tab) (or (and (bufferp tab) tab) | 921 | (lambda (tab) (or (and (bufferp tab) tab) |
| 910 | (alist-get 'buffer tab))) | 922 | (alist-get 'buffer tab))) |
| 911 | (funcall tab-line-tabs-function))) | 923 | (funcall tab-line-tabs-function))) |
| 912 | (pos (seq-position buffers (current-buffer))) | 924 | (old-pos (seq-position buffers (current-buffer))) |
| 913 | (buffer (when pos | 925 | (new-pos (when old-pos (+ old-pos (or arg 1)))) |
| 914 | (if (and tab-line-switch-cycling | 926 | (new-pos (when new-pos |
| 915 | (<= (length buffers) (1+ pos))) | 927 | (if tab-line-switch-cycling |
| 916 | (car buffers) | 928 | (mod new-pos (length buffers)) |
| 917 | (nth (1+ pos) buffers))))) | 929 | (min new-pos (1- (length buffers)))))) |
| 930 | (buffer (when new-pos (nth new-pos buffers)))) | ||
| 918 | (when (bufferp buffer) | 931 | (when (bufferp buffer) |
| 919 | (let ((switch-to-buffer-obey-display-actions nil)) | 932 | (let ((switch-to-buffer-obey-display-actions nil)) |
| 920 | (switch-to-buffer buffer)))))))) | 933 | (switch-to-buffer buffer)))))))) |