Skip to content

Commit ac9acc1

Browse files
committed
Use quit-restore-window to close tab (bug#39446)
* lisp/tab-bar.el (display-buffer-in-new-tab): New function with code from display-buffer-in-tab. (display-buffer-in-tab): Call display-buffer-in-new-tab. (switch-to-buffer-other-tab): Instead of 'display-buffer-same-window' use '(reusable-frames . t)'. * lisp/windmove.el (windmove-display-in-direction): Set arg 'type' to 'tab' for window--display-buffer when creating a new tab. * lisp/window.el (quit-restore-window): Call tab-bar-close-tab when quit-restore type is 'tab'. (display-buffer-record-window): Set window-parameter 'quit-restore' to 'tab' for type 'tab'. (window--display-buffer): Set window-prev-buffers to nil for tab too.
1 parent 5e7bead commit ac9acc1

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

lisp/tab-bar.el

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,8 +1467,7 @@ This is an action function for buffer display, see Info
14671467
node `(elisp) Buffer Display Action Functions'. It should be
14681468
called only by `display-buffer' or a function directly or
14691469
indirectly called by the latter."
1470-
(let* ((tab-name (alist-get 'tab-name alist))
1471-
(reusable-frames (alist-get 'reusable-frames alist))
1470+
(let* ((reusable-frames (alist-get 'reusable-frames alist))
14721471
(reusable-tab (when reusable-frames
14731472
(tab-bar-get-buffer-tab buffer reusable-frames))))
14741473
(if reusable-tab
@@ -1480,26 +1479,55 @@ indirectly called by the latter."
14801479
(tab-bar-select-tab (1+ index)))
14811480
(when (get-buffer-window buffer frame)
14821481
(select-window (get-buffer-window buffer frame))))
1482+
(let ((tab-name (alist-get 'tab-name alist)))
1483+
(when (functionp tab-name)
1484+
(setq tab-name (funcall tab-name buffer alist)))
1485+
(if tab-name
1486+
(let ((tab-index (tab-bar--tab-index-by-name tab-name)))
1487+
(if tab-index
1488+
(progn
1489+
(tab-bar-select-tab (1+ tab-index))
1490+
(when (get-buffer-window buffer)
1491+
(select-window (get-buffer-window buffer))))
1492+
(display-buffer-in-new-tab buffer alist)))
1493+
(display-buffer-in-new-tab buffer alist))))))
1494+
1495+
(defun display-buffer-in-new-tab (buffer alist)
1496+
"Display BUFFER in a new tab.
1497+
ALIST is an association list of action symbols and values. See
1498+
Info node `(elisp) Buffer Display Action Alists' for details of
1499+
such alists.
1500+
1501+
Like `display-buffer-in-tab', but always creates a new tab unconditionally,
1502+
without checking if a suitable tab already exists.
1503+
1504+
If ALIST contains a `tab-name' entry, it creates a new tab with that name
1505+
and displays BUFFER in a new tab. The `tab-name' entry can be a function,
1506+
then it is called with two arguments: BUFFER and ALIST, and should return
1507+
the tab name. When a `tab-name' entry is omitted, create a new tab without
1508+
an explicit name.
1509+
1510+
This is an action function for buffer display, see Info
1511+
node `(elisp) Buffer Display Action Functions'. It should be
1512+
called only by `display-buffer' or a function directly or
1513+
indirectly called by the latter."
1514+
(let ((tab-bar-new-tab-choice t))
1515+
(tab-bar-new-tab)
1516+
(let ((tab-name (alist-get 'tab-name alist)))
14831517
(when (functionp tab-name)
14841518
(setq tab-name (funcall tab-name buffer alist)))
1485-
(if tab-name
1486-
(let ((tab-index (tab-bar--tab-index-by-name tab-name)))
1487-
(if tab-index
1488-
(tab-bar-select-tab (1+ tab-index))
1489-
(let ((tab-bar-new-tab-choice t))
1490-
(tab-bar-new-tab)
1491-
(tab-bar-rename-tab tab-name))))
1492-
(let ((tab-bar-new-tab-choice t))
1493-
(tab-bar-new-tab))))))
1519+
(when tab-name
1520+
(tab-bar-rename-tab tab-name)))
1521+
(window--display-buffer buffer (selected-window) 'tab alist)))
14941522

14951523
(defun switch-to-buffer-other-tab (buffer-or-name &optional norecord)
14961524
"Switch to buffer BUFFER-OR-NAME in another tab.
14971525
Like \\[switch-to-buffer-other-frame] (which see), but creates a new tab."
14981526
(interactive
14991527
(list (read-buffer-to-switch "Switch to buffer in other tab: ")))
1500-
(display-buffer buffer-or-name '((display-buffer-in-tab
1501-
display-buffer-same-window)
1502-
(inhibit-same-window . nil))
1528+
(display-buffer buffer-or-name '((display-buffer-in-tab)
1529+
(inhibit-same-window . nil)
1530+
(reusable-frames . t))
15031531
norecord))
15041532

15051533
(defun find-file-other-tab (filename &optional wildcards)

lisp/windmove.el

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,19 @@ When `switch-to-buffer-obey-display-actions' is non-nil,
467467
(minibuffer-depth (minibuffer-depth))
468468
(action (lambda (buffer alist)
469469
(unless (> (minibuffer-depth) minibuffer-depth)
470-
(let ((window (cond
471-
((eq dir 'new-tab)
472-
(let ((tab-bar-new-tab-choice t))
473-
(tab-bar-new-tab))
474-
(selected-window))
475-
((eq dir 'same-window)
476-
(selected-window))
477-
(t (window-in-direction
478-
dir nil nil
479-
(and arg (prefix-numeric-value arg))
480-
windmove-wrap-around))))
481-
(type 'reuse))
470+
(let* ((type 'reuse)
471+
(window (cond
472+
((eq dir 'new-tab)
473+
(let ((tab-bar-new-tab-choice t))
474+
(tab-bar-new-tab))
475+
(setq type 'tab)
476+
(selected-window))
477+
((eq dir 'same-window)
478+
(selected-window))
479+
(t (window-in-direction
480+
dir nil nil
481+
(and arg (prefix-numeric-value arg))
482+
windmove-wrap-around)))))
482483
(unless window
483484
(setq window (split-window nil nil dir) type 'window))
484485
(setq new-window (window--display-buffer buffer window

lisp/window.el

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,6 +5008,13 @@ nil means to not handle the buffer in a particular way. This
50085008
prev-buffer)))
50095009
quad entry)
50105010
(cond
5011+
((and (not prev-buffer)
5012+
(eq (nth 1 quit-restore) 'tab)
5013+
(eq (nth 3 quit-restore) buffer))
5014+
(tab-bar-close-tab)
5015+
;; If the previously selected window is still alive, select it.
5016+
(when (window-live-p (nth 2 quit-restore))
5017+
(select-window (nth 2 quit-restore))))
50115018
((and (not prev-buffer)
50125019
(or (eq (nth 1 quit-restore) 'frame)
50135020
(and (eq (nth 1 quit-restore) 'window)
@@ -6367,7 +6374,12 @@ fourth element is BUFFER."
63676374
;; WINDOW has been created on a new frame.
63686375
(set-window-parameter
63696376
window 'quit-restore
6370-
(list 'frame 'frame (selected-window) buffer)))))
6377+
(list 'frame 'frame (selected-window) buffer)))
6378+
((eq type 'tab)
6379+
;; WINDOW has been created on a new tab.
6380+
(set-window-parameter
6381+
window 'quit-restore
6382+
(list 'tab 'tab (selected-window) buffer)))))
63716383

63726384
(defcustom display-buffer-function nil
63736385
"If non-nil, function to call to handle `display-buffer'.
@@ -7034,7 +7046,7 @@ Return WINDOW if BUFFER and WINDOW are live."
70347046
;; use that.
70357047
(display-buffer-mark-dedicated
70367048
(set-window-dedicated-p window display-buffer-mark-dedicated))))
7037-
(when (memq type '(window frame))
7049+
(when (memq type '(window frame tab))
70387050
(set-window-prev-buffers window nil))
70397051
(let ((quit-restore (window-parameter window 'quit-restore))
70407052
(height (cdr (assq 'window-height alist)))

0 commit comments

Comments
 (0)