]> git.eshelyaron.com Git - emacs.git/commitdiff
Show stash counts in button in vc-dir
authorRobert Pluim <rpluim@gmail.com>
Tue, 22 Oct 2019 07:31:15 +0000 (09:31 +0200)
committerRobert Pluim <rpluim@gmail.com>
Tue, 22 Oct 2019 07:36:17 +0000 (09:36 +0200)
Based on suggestions from Mattias EngdegĂ„rd.

* lisp/vc/vc-git.el (vc-git--make-button-text): New function to
generate text for stash button.
(vc-git-make-stash-button): Show stash counts.  Delete and recreate
button when toggling.
(vc-git-dir-extra-headers): Pass counts to vc-git-make-stash-button.
Treat stash count <= vc-git-show-stash as equivalent to showing entire
list.

lisp/vc/vc-git.el

index 70f53990733a869e5260adfc086d2e5ec2e231b4..ce8e57df7680d503f8205d29a9a78bb9137afc9b 100644 (file)
@@ -650,17 +650,34 @@ or an empty string if none."
     (define-key map "S" 'vc-git-stash-snapshot)
     map))
 
-(defun vc-git-make-stash-button ()
-    (make-text-button "Show/hide stashes" nil
-                      'action
-                      (lambda (&rest _ignore)
-                          (let* ((inhibit-read-only t)
-                                 (start (next-single-property-change (point-min) 'vc-git-hideable))
-                                 (end (next-single-property-change start 'vc-git-hideable)))
-                            (add-text-properties
-                             start end
-                             `(invisible ,(not (get-text-property start 'invisible))))))
-                      'help-echo "mouse-2, RET: Show/hide stashes"))
+(defun vc-git--make-button-text (show count1 count2)
+  (if show
+      (format "Show all stashes (%s)" count2)
+    (if (= count1 count2)
+        (format "Hide all stashes (%s)" count2)
+      (format "Show %s stash%s (of %s)" count1 (if (= count1 1) "" "es") count2))))
+
+(defun vc-git-make-stash-button (show count1 count2)
+  (let ((orig-text (vc-git--make-button-text show count1 count2)))
+    (make-text-button
+     orig-text nil
+     'action
+     (lambda (counts)
+       (let* ((inhibit-read-only t)
+              (start (next-single-property-change
+                      (point-min) 'vc-git-hideable))
+              (end (next-single-property-change
+                    start 'vc-git-hideable))
+              (state (get-text-property start 'invisible)))
+         (add-text-properties
+          start end
+          `(invisible ,(not state)))
+         (save-excursion
+           (delete-region (button-start (point)) (button-end (point)))
+           (insert (vc-git-make-stash-button
+                    (not state) (car counts) (cdr counts))))))
+     'button-data (cons count1 count2)
+     'help-echo "mouse-2, RET: Show/hide stashes")))
 
 (defvar vc-git-stash-menu-map
   (let ((map (make-sparse-keymap "Git Stash")))
@@ -707,14 +724,18 @@ or an empty string if none."
            (setq remote-url (match-string 1 remote-url))))
       (setq branch "not (detached HEAD)"))
     (when stash-list
-      (let* ((limit
+      (let* ((len (length stash-list))
+             (limit
               (if (integerp vc-git-show-stash)
-                  (min vc-git-show-stash (length stash-list))
-                (length stash-list)))
+                  (min vc-git-show-stash len)
+                len))
              (shown-stashes (cl-subseq stash-list 0 limit))
              (hidden-stashes (cl-subseq stash-list limit))
-             (all-hideable (eq vc-git-show-stash t)))
-        (setq stash-button (vc-git-make-stash-button)
+             (all-hideable (or (eq vc-git-show-stash t)
+                               (<= len vc-git-show-stash))))
+        (setq stash-button (if all-hideable
+                               (vc-git-make-stash-button nil limit limit)
+                             (vc-git-make-stash-button t vc-git-show-stash len))
               stash-string
               (concat
                (when shown-stashes