]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/tab-bar.el (tab-bar-select-restore-windows): New defcustom.
authorJuri Linkov <juri@linkov.net>
Sun, 17 Mar 2024 17:57:05 +0000 (19:57 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 18 Mar 2024 15:44:34 +0000 (16:44 +0100)
(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).

(cherry picked from commit c29b6df2273347946d5b8c88b5dee39d8d6fd202)

etc/NEWS
lisp/tab-bar.el

index 1335411b2605dd211efc997dfcaa0ba951962bb3..505abed80330facf4d1fafd333356575b22814ed 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -276,6 +276,13 @@ corresponding configuration or state was recorded.
 
 ** Tab Bars and Tab Lines
 
+---
+*** New user option 'tab-bar-select-restore-windows'.
+It defines what to do with windows whose buffer was killed
+since the tab was last selected.  By default it displays
+a placeholder buffer that provides information about the name
+of the killed buffer that was displayed in that window.
+
 ---
 *** New user option 'tab-bar-tab-name-format-functions'.
 It can be used to add, remove and reorder functions that change
index 61efa332e0b73bb774b6f971fac3e25f3abe2ddf..fa22500a04efbf3c539dc8ae2a3800008398c6ad 100644 (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