Function: tab-bar-select-tab

tab-bar-select-tab is an interactive and byte-compiled function defined in tab-bar.el.gz.

Signature

(tab-bar-select-tab &optional TAB-NUMBER)

Documentation

Switch to the tab by its absolute position TAB-NUMBER in the tab bar.

When this command is bound to a numeric key (with a key prefix or modifier key using tab-bar-select-tab-modifiers), calling it without an argument will translate its bound numeric key to the numeric argument. Also the prefix argument TAB-NUMBER can be used to override the numeric key, so it takes precedence over the bound digit key. For example, <MODIFIER>-2 will select the second tab, but C-u 15
<MODIFIER>-2 will select the 15th tab. TAB-NUMBER counts from 1.
Negative TAB-NUMBER counts tabs from the end of the tab bar.

Key Bindings

Aliases

tab-select

Source Code

;; Defined in /usr/src/emacs/lisp/tab-bar.el.gz
(defun tab-bar-select-tab (&optional tab-number)
  "Switch to the tab by its absolute position TAB-NUMBER in the tab bar.
When this command is bound to a numeric key (with a key prefix or modifier key
using `tab-bar-select-tab-modifiers'), calling it without an argument
will translate its bound numeric key to the numeric argument.
Also the prefix argument TAB-NUMBER can be used to override
the numeric key, so it takes precedence over the bound digit key.
For example, `<MODIFIER>-2' will select the second tab, but `C-u 15
<MODIFIER>-2' will select the 15th tab.  TAB-NUMBER counts from 1.
Negative TAB-NUMBER counts tabs from the end of the tab bar."
  (interactive "P")
  (unless (integerp tab-number)
    (let ((key (event-basic-type last-command-event)))
      (setq tab-number (if (and (characterp key) (>= key ?1) (<= key ?9))
                           (- key ?0)
                         0))))

  (let* ((tabs (funcall tab-bar-tabs-function))
         (from-index (tab-bar--current-tab-index tabs))
         (to-number (cond ((< tab-number 0) (+ (length tabs) (1+ tab-number)))
                          ((zerop tab-number) (1+ from-index))
                          (t tab-number)))
         (to-index (1- (max 1 (min to-number (length tabs)))))
         (minibuffer-was-active (minibuffer-window-active-p (selected-window))))

    (when (and read-minibuffer-restore-windows minibuffer-was-active
               (not tab-bar-minibuffer-restore-tab))
      (setq-local tab-bar-minibuffer-restore-tab (1+ from-index))
      (add-hook 'minibuffer-exit-hook #'tab-bar-minibuffer-restore-tab nil t))

    (unless (eq from-index to-index)
      (let* ((from-tab (tab-bar--tab))
             (to-tab (nth to-index tabs))
             (wc (alist-get 'wc to-tab))
             (ws (alist-get 'ws to-tab))
             (window-restore-killed-buffer-windows
              (or tab-bar-select-restore-windows
                  window-restore-killed-buffer-windows)))

        ;; During the same session, use window-configuration to switch
        ;; tabs, because window-configurations are more reliable
        ;; (they keep references to live buffers) than window-states.
        ;; But after restoring tabs from a previously saved session,
        ;; its value of window-configuration is unreadable,
        ;; so restore its saved window-state.
        (cond
         ((and (window-configuration-p wc)
               ;; Check for such cases as cloning a frame with tabs.
               ;; When tabs were cloned to another frame, then fall back
               ;; to using `window-state-put' below.
               (eq (window-configuration-frame wc) (selected-frame)))
          (let ((wc-point (alist-get 'wc-point to-tab))
                (wc-bl  (seq-filter #'buffer-live-p (alist-get 'wc-bl to-tab)))
                (wc-bbl (seq-filter #'buffer-live-p (alist-get 'wc-bbl to-tab)))
                (wc-history-back (alist-get 'wc-history-back to-tab))
                (wc-history-forward (alist-get 'wc-history-forward to-tab)))

            (set-window-configuration wc nil t)

            ;; set-window-configuration does not restore the value of
            ;; point in the current buffer, so restore it separately.
            (when (and (markerp wc-point)
                       (marker-buffer wc-point))
              (goto-char wc-point))

            (set-frame-parameter nil 'buffer-list wc-bl)
            (set-frame-parameter nil 'buried-buffer-list wc-bbl)

            (when tab-bar-history-mode
              (puthash (selected-frame)
                       (and (window-configuration-p
                             (alist-get 'wc (car wc-history-back)))
                            wc-history-back)
                       tab-bar-history-back)
              (puthash (selected-frame)
                       (and (window-configuration-p
                             (alist-get 'wc (car wc-history-forward)))
                            wc-history-forward)
                       tab-bar-history-forward))))

         (ws
          ;; `window-state-put' fails when called in the minibuffer
          (when (window-minibuffer-p)
            (select-window (get-mru-window)))
          (window-state-put ws nil 'safe)

          (when tab-bar-history-mode
            (puthash (selected-frame) nil tab-bar-history-back)
            (puthash (selected-frame) nil tab-bar-history-forward))))

        (when tab-bar-select-restore-context
          (window-point-context-use))

        ;; Select the minibuffer when it was active before switching tabs
        (when (and minibuffer-was-active (active-minibuffer-window))
          (select-window (active-minibuffer-window)))

        ;; When the minibuffer was activated in one tab, but exited in
        ;; another tab, then after going back to the first tab, it has
        ;; such inconsistent state that the current buffer is the minibuffer,
        ;; but its window is not active.  So try to undo this mess.
        (when (and (window-minibuffer-p) (not (active-minibuffer-window)))
          (select-window (get-mru-window)))

        (when tab-bar-history-mode
          (setq tab-bar-history-omit t))

        (when from-index
          (setf (nth from-index tabs) from-tab))
        (setf (nth to-index tabs)
              (tab-bar--current-tab-make (nth to-index tabs)))

        (unless tab-bar-mode
          (message "Selected tab '%s'" (alist-get 'name to-tab)))

        (run-hook-with-args 'tab-bar-tab-post-select-functions
                            from-tab to-tab))

      (force-mode-line-update))))