1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

* lisp/tab-bar.el (tab-bar-select-restore-windows): New defcustom.

(tab-bar-select-restore-windows): New function.
(tab-bar-select-tab): Let-bind window-restore-killed-buffer-windows
to tab-bar-select-restore-windows (bug#68235).
This commit is contained in:
Juri Linkov 2024-03-17 19:57:05 +02:00
parent 562d9c9db5
commit c29b6df227
2 changed files with 60 additions and 1 deletions

View file

@ -1393,6 +1393,55 @@ and the newly selected tab."
:group 'tab-bar
:version "30.1")
(defcustom tab-bar-select-restore-windows #'tab-bar-select-restore-windows
"Function called when selecting a tab to handle windows whose buffer was killed.
When a tab-bar tab displays a window whose buffer was killed since
this tab was last selected, this function determines what to do with
that window. By default, either a random buffer is displayed instead of
the killed buffer, or the window gets deleted. However, with the help
of `window-restore-killed-buffer-windows' it's possible to handle such
situations better by displaying an information about the killed buffer."
:type '(choice (const :tag "No special handling" nil)
(const :tag "Show placeholder buffers"
tab-bar-select-restore-windows)
(function :tag "Function"))
:group 'tab-bar
:version "30.1")
(defun tab-bar-select-restore-windows (_frame windows _type)
"Display a placeholder buffer in the window whose buffer was killed.
A button in the window allows to restore the killed buffer,
if it was visiting a file."
(dolist (quad windows)
(when (window-live-p (nth 0 quad))
(let* ((window (nth 0 quad))
(old-buffer (nth 1 quad))
(file (when (bufferp old-buffer)
(buffer-file-name old-buffer)))
(name (or file
(and (bufferp old-buffer)
(fboundp 'buffer-last-name)
(buffer-last-name old-buffer))
old-buffer))
(new-buffer (generate-new-buffer
(format "*Old buffer %s*" name))))
(with-current-buffer new-buffer
(set-auto-mode)
(insert (format-message "This window displayed the %s `%s'.\n"
(if file "file" "buffer")
name))
(when file
(insert-button
"[Restore]" 'action
(lambda (_button)
(set-window-buffer window (find-file-noselect file))
(set-window-start window (nth 2 quad) t)
(set-window-point window (nth 3 quad))))
(insert "\n"))
(goto-char (point-min))
(setq buffer-read-only t)
(set-window-buffer window new-buffer))))))
(defvar tab-bar-minibuffer-restore-tab nil
"Tab number for `tab-bar-minibuffer-restore-tab'.")
@ -1438,7 +1487,10 @@ Negative TAB-NUMBER counts tabs from the end of the tab bar."
(let* ((from-tab (tab-bar--tab))
(to-tab (nth to-index tabs))
(wc (alist-get 'wc to-tab))
(ws (alist-get 'ws 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