branch: elpa/git-commit
commit e42121aaf13fad812b581938c5bc71139beec6e2
Author: Jonas Bernoulli <jo...@bernoul.li>
Commit: Jonas Bernoulli <jo...@bernoul.li>

    magit-cancel-section: Add IF-EMPTY argument
---
 lisp/magit-section.el   | 17 +++++++++++++----
 lisp/magit-stash.el     |  8 +++-----
 lisp/magit-submodule.el |  8 +++-----
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/lisp/magit-section.el b/lisp/magit-section.el
index 22e54beb26..e772d110c9 100644
--- a/lisp/magit-section.el
+++ b/lisp/magit-section.el
@@ -1484,11 +1484,20 @@ anything this time around.
            (oset ,s children (nreverse (oref ,s children))))
          ,s))))
 
-(defun magit-cancel-section ()
+(defun magit-cancel-section (&optional if-empty)
   "Cancel inserting the section that is currently being inserted.
-Remove all traces of that section."
-  (when magit-insert-section--current
-    (if (not (oref magit-insert-section--current parent))
+
+Canceling returns from the inner most use of `magit-insert-section' and
+removes all text that was inserted by that.
+
+If optional IF-EMPTY is non-nil, then only cancel the section, if it is
+empty.  If a section is split into a heading and a body (i.e., when its
+`content' slot is non-nil), then only check if the body is empty."
+  (when (and magit-insert-section--current
+             (or (not if-empty)
+                 (= (point) (or (oref magit-insert-section--current content)
+                                (oref magit-insert-section--current start)))))
+    (if (eq magit-insert-section--current magit-root-section)
         (insert "(empty)\n")
       (delete-region (oref magit-insert-section--current start)
                      (point))
diff --git a/lisp/magit-stash.el b/lisp/magit-stash.el
index cbeaea1587..65f2189208 100644
--- a/lisp/magit-stash.el
+++ b/lisp/magit-stash.el
@@ -607,13 +607,11 @@ If there is no stash buffer in the same frame, then do 
nothing."
   "Insert section showing notes for a stash.
 This shows the notes for stash@{N} but not for the other commits
 that make up the stash."
-  (magit-insert-section section (note)
+  (magit-insert-section (note)
     (magit-insert-heading "Notes")
     (magit-git-insert "notes" "show" magit-buffer-revision)
-    (if (= (point)
-           (oref section content))
-        (magit-cancel-section)
-      (insert "\n"))))
+    (magit-cancel-section 'if-empty)
+    (insert "\n")))
 
 (defun magit-insert-stash-index ()
   "Insert section showing staged changes of the stash."
diff --git a/lisp/magit-submodule.el b/lisp/magit-submodule.el
index d2cb6b9a7d..73f7442b49 100644
--- a/lisp/magit-submodule.el
+++ b/lisp/magit-submodule.el
@@ -590,7 +590,7 @@ These sections can be expanded to show the respective 
commits."
   "For internal use, don't add to a hook."
   (unless (magit-ignore-submodules-p)
     (when-let ((modules (magit-list-module-paths)))
-      (magit-insert-section section ((eval type) nil t)
+      (magit-insert-section ((eval type) nil t)
         (string-match "\\`\\(.+\\) \\([^ ]+\\)\\'" heading)
         (magit-insert-heading
           (propertize (match-string 1 heading)
@@ -617,10 +617,8 @@ These sections can be expanded to show the respective 
commits."
                     (when (> (point)
                              (oref sec content))
                       (delete-char -1))))))))
-        (if (> (point)
-               (oref section content))
-            (insert ?\n)
-          (magit-cancel-section))))))
+        (magit-cancel-section 'if-empty)
+        (insert ?\n)))))
 
 ;;; List
 

Reply via email to