Function: tab-bar-close-other-tabs

tab-bar-close-other-tabs is an interactive and byte-compiled function defined in tab-bar.el.gz.

Signature

(tab-bar-close-other-tabs &optional TAB-NUMBER)

Documentation

Close all tabs on the selected frame, except the tab TAB-NUMBER.

TAB-NUMBER counts from 1 and defaults to the current tab (which happens interactively).

Key Bindings

Aliases

tab-close-other

Source Code

;; Defined in /usr/src/emacs/lisp/tab-bar.el.gz
(defun tab-bar-close-other-tabs (&optional tab-number)
  "Close all tabs on the selected frame, except the tab TAB-NUMBER.
TAB-NUMBER counts from 1 and defaults to the current tab (which
happens interactively)."
  (interactive)
  (let* ((tabs (funcall tab-bar-tabs-function))
         (current-index (tab-bar--current-tab-index tabs))
         (keep-index (if (integerp tab-number)
                         (1- (max 1 (min tab-number (length tabs))))
                       current-index))
         (index 0))

    (when (nth keep-index tabs)
      (unless (eq keep-index current-index)
        (tab-bar-select-tab (1+ keep-index))
        (setq tabs (funcall tab-bar-tabs-function)))

      (dolist (tab tabs)
        (unless (or (eq index keep-index)
                    (run-hook-with-args-until-success
                     'tab-bar-tab-prevent-close-functions tab
                     ;; `last-tab-p' logically can't ever be true
                     ;; if we make it this far
                     nil))
          (push `((frame . ,(selected-frame))
                  (index . ,index)
                  (tab . ,tab))
                tab-bar-closed-tabs)
          (run-hook-with-args 'tab-bar-tab-pre-close-functions tab nil)
          (setq tabs (delq tab tabs)))
        (setq index (1+ index)))
      (tab-bar-tabs-set tabs)

      ;; Recalculate tab-bar-lines and update frames
      (tab-bar--update-tab-bar-lines)

      (force-mode-line-update)
      (unless tab-bar-mode
        (message "Deleted all other tabs")))))