branch: elpa/magit
commit 731642756f504c8a56d3775960b7af0a93c618bb
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    Don't kill blob buffers when displayed in multiple windows
    
    Previously "q" in a blob buffer unconditionally killed it.  I have
    a personal command that displays the current buffer in a new frame
    (while still displaying it in the old window/frame), which I often
    follow up by quitting the current buffer in the old window, which
    then ends up killing the buffer "in both frames".
    
    On the other hand, when time traveling, it is useful if "q" cleans
    up completely, else we would end up with many blob buffers, which
    are no longer of interest.
    
    Determining whether to kill or only bury the current buffer based on
    whether it is displayed in a second window, covers both use-cases.
    
    Other users might find different behaviors useful, so implement and
    mention some alternatives.
---
 docs/magit.org      | 14 ++++++++++----
 docs/magit.texi     | 16 +++++++++++-----
 lisp/magit-base.el  |  5 -----
 lisp/magit-files.el | 22 +++++++++++++++++++++-
 4 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/docs/magit.org b/docs/magit.org
index 4acd60006f8..95b981f5276 100644
--- a/docs/magit.org
+++ b/docs/magit.org
@@ -8075,15 +8075,21 @@ bindings, but this might be extended.
 
 - Key: p (magit-blob-previous) ::
 
-  Visit the previous blob which modified the current file.
+  This command visits the previous blob that modified the current
+  file.
 
 - Key: n (magit-blob-next) ::
 
-  Visit the next blob which modified the current file.
+  This command visit the next blob that modified the current file.
+
+- Key: q (magit-bury-or-kill-buffer) ::
 
-- Key: q (magit-kill-this-buffer) ::
+  This command buries the current buffer, if that is being displayed
+  in multiple windows and/or when a prefix argument is used.  If
+  neither is the case, it instead kills the current buffer.
 
-  Kill the current buffer.
+You might want to bind ~u~ to another command.  Suitable commands
+include ~bury-buffer~, ~magit-bury-buffer~ and ~magit-kill-this-buffer~.
 
 * Customizing
 
diff --git a/docs/magit.texi b/docs/magit.texi
index 9fc283b1678..3e3f92a370a 100644
--- a/docs/magit.texi
+++ b/docs/magit.texi
@@ -9755,19 +9755,25 @@ bindings, but this might be extended.
 @item @kbd{p} (@code{magit-blob-previous})
 @kindex p
 @findex magit-blob-previous
-Visit the previous blob which modified the current file.
+This command visits the previous blob that modified the current
+file.
 
 @item @kbd{n} (@code{magit-blob-next})
 @kindex n
 @findex magit-blob-next
-Visit the next blob which modified the current file.
+This command visit the next blob that modified the current file.
 
-@item @kbd{q} (@code{magit-kill-this-buffer})
+@item @kbd{q} (@code{magit-bury-or-kill-buffer})
 @kindex q
-@findex magit-kill-this-buffer
-Kill the current buffer.
+@findex magit-bury-or-kill-buffer
+This command buries the current buffer, if that is being displayed
+in multiple windows and/or when a prefix argument is used.  If
+neither is the case, it instead kills the current buffer.
 @end table
 
+You might want to bind @code{u} to another command.  Suitable commands
+include @code{bury-buffer}, @code{magit-bury-buffer} and 
@code{magit-kill-this-buffer}.
+
 @node Customizing
 @chapter Customizing
 
diff --git a/lisp/magit-base.el b/lisp/magit-base.el
index 402454653a9..2053d9a7675 100644
--- a/lisp/magit-base.el
+++ b/lisp/magit-base.el
@@ -974,11 +974,6 @@ Pad the left side of STRING so that it aligns with the 
text area."
 
 ;;; Missing from Emacs
 
-(defun magit-kill-this-buffer ()
-  "Kill the current buffer."
-  (interactive)
-  (kill-buffer (current-buffer)))
-
 (defun magit--buffer-string (&optional min max trim)
   "Like `buffer-substring-no-properties' but the arguments are optional.
 
diff --git a/lisp/magit-files.el b/lisp/magit-files.el
index ed65c0ea399..60076af08a7 100644
--- a/lisp/magit-files.el
+++ b/lisp/magit-files.el
@@ -355,7 +355,7 @@ to `magit-dispatch'."
   "b" #'magit-blame-addition
   "r" #'magit-blame-removal
   "f" #'magit-blame-reverse
-  "q" #'magit-kill-this-buffer)
+  "q" #'magit-bury-or-kill-buffer)
 
 (define-minor-mode magit-blob-mode
   "Enable some Magit features in blob-visiting buffers.
@@ -364,6 +364,26 @@ Currently this only adds the following key bindings.
 \n\\{magit-blob-mode-map}"
   :package-version '(magit . "2.3.0"))
 
+(defun magit-bury-buffer (&optional kill-buffer)
+  "Bury the current buffer, or with a prefix argument kill it."
+  (interactive "P")
+  (if kill-buffer (kill-buffer) (bury-buffer)))
+
+(defun magit-bury-or-kill-buffer (&optional bury-buffer)
+  "Bury the current buffer if displayed in multiple windows, else kill it.
+With a prefix argument only bury the buffer even if it is only displayed
+in a single window."
+  (interactive "P")
+  (if (and (cdr (get-buffer-window-list nil nil t))
+           (not bury-buffer))
+      (kill-buffer)
+    (bury-buffer)))
+
+(defun magit-kill-this-buffer ()
+  "Kill the current buffer."
+  (interactive)
+  (kill-buffer))
+
 (defun magit-blob-next ()
   "Visit the next blob which modified the current file."
   (interactive)

Reply via email to