diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 5eb332884c..af993637a6 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -1284,6 +1284,44 @@ display-buffer-in-tab (tab-bar-rename-tab name)))) (tab-bar-new-tab)))) +(defun tab-bar-get-buffer-tab (buffer-or-name &optional all-frames) + "Return a tab currently displaying BUFFER-OR-NAME, or nil if none. +BUFFER-OR-NAME may be a buffer or a buffer name and defaults to +the current buffer. + +The optional argument ALL-FRAMES specifies the frames to consider: + +- t means consider all tabs on all existing frames. + +- `visible' means consider all tabs on all visible frames. + +- A frame means consider all tabs on that frame only. + +Any other value of ALL-FRAMES means consider all tabs on the +selected frame and no others." + (let ((buffer (if buffer-or-name + (get-buffer buffer-or-name) + (current-buffer)))) + (when (bufferp buffer) + (let ((frames (cond + ((eq all-frames t) (frame-list)) + ((eq all-frames 'visible) (visible-frame-list)) + ((framep all-frames) (list all-frames)) + (t (list (selected-frame)))))) + (seq-some (lambda (frame) + (with-selected-frame frame + (seq-some (lambda (tab) + (when (if (eq (car tab) 'current-tab) + (get-buffer-window buffer frame) + (let* ((state (cdr (assq 'ws tab))) + (buffers (when state + (window-state-buffers state)))) + (or (memq buffer buffers) + (member (buffer-name buffer) buffers)))) + (append tab `((frame . ,frame))))) + (funcall tab-bar-tabs-function)))) + frames))))) + (defun switch-to-buffer-other-tab (buffer-or-name &optional norecord) "Switch to buffer BUFFER-OR-NAME in another tab.