branch: externals/org
commit 8e2c28d1b1567b01c44754a623bac9fe6edc05fd
Author: Jacob S. Gordon <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    Ensure a non-zero percent cookie when anything is complete
    
    With a large number of tasks (N > 100) the percent cookie can show 0%
    when a non-zero amount have been completed, e.g. 1/101.  In those
    cases adjust the percent to show 1% instead.
    
    * lisp/org-macs.el (org-format-percent-cookie): Add function.
    * lisp/org.el (org-update-parent-todo-statistics):
    * lisp/org-list.el (org-update-checkbox-count):
    * lisp/org-colview.el (org-columns--summary-checkbox-percent): Use
    function.
    * testing/lisp/test-org-list.el (test-org-list/update-checkbox-count):
    * testing/lisp/test-org.el (test-org/update-todo-statistics-cookies):
    Add tests.
    * etc/ORG-NEWS: Announce changes.
---
 etc/ORG-NEWS                  | 12 +++++++++
 lisp/org-colview.el           |  7 +++---
 lisp/org-list.el              |  3 +--
 lisp/org-macs.el              |  7 ++++++
 lisp/org.el                   |  5 ++--
 testing/lisp/test-org-list.el | 20 ++++++++++++++-
 testing/lisp/test-org.el      | 57 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 101 insertions(+), 10 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 7eced37470..b125558b04 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -40,10 +40,22 @@ Please send Org bug reports to mailto:[email protected].
 
 # This also includes changes in function behavior from Elisp perspective.
 
+*** New function ~org-format-percent-cookie~
+
+Given the completed and total number of tasks, format the percent
+cookie =[N%]=.
+
 ** Removed or renamed functions and variables
 
 ** Miscellaneous
 
+*** The percent cookie is now non-zero when any sub-task is complete
+
+With a large number of tasks (N > 100) the percent cookie previously
+showed =0%= when a non-zero amount have been completed, e.g. 1/101.
+Those cases now show =1%= instead.  The same is true for the checkbox
+status summary type =X%= when defining columns for column view.
+
 * Version 9.8
 ** Important announcements and breaking changes
 
diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index 8b97aa2ad0..5df8841997 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -1400,10 +1400,9 @@ When FMT is non-nil, use it to format the result."
 
 (defun org-columns--summary-checkbox-percent (check-boxes _)
   "Summarize CHECK-BOXES with a check-box percent."
-  (format "[%d%%]"
-         (round (* 100.0 (cl-count-if (lambda (b) (member b '("[X]" "[100%]")))
-                                      check-boxes))
-                (length check-boxes))))
+  (org-format-percent-cookie (cl-count-if (lambda (b) (member b '("[X]" 
"[100%]")))
+                                          check-boxes)
+                             (length check-boxes)))
 
 (defun org-columns--summary-min (values fmt)
   "Compute the minimum of VALUES.
diff --git a/lisp/org-list.el b/lisp/org-list.el
index 687098c1c1..147c039fe5 100644
--- a/lisp/org-list.el
+++ b/lisp/org-list.el
@@ -2656,8 +2656,7 @@ portion of the buffer."
            (goto-char beg)
             (org-fold-core-ignore-modifications
              (insert-and-inherit
-              (if percent (format "[%d%%]" (floor (* 100.0 checked)
-                                                  (max 1 total)))
+              (if percent (org-format-percent-cookie checked total)
                 (format "[%d/%d]" checked total)))
              (delete-region (point) (+ (point) (- end beg))))
            (when org-auto-align-tags (org-fix-tags-on-the-fly))))))))
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 74d7902ba6..9c7174efe4 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1873,6 +1873,13 @@ transient buffer) then return nil."
       (buffer-file-name base-buffer)
     (buffer-file-name buffer)))
 
+(defun org-format-percent-cookie (completed total)
+  "Format the percent cookie `[N%]' for COMPLETED/TOTAL tasks.
+The percent is rounded down to the nearest integer, except for small
+percentages 0% < p < 1% which are rounded up to 1%."
+  (let ((p (floor (* 100.0 completed) (max 1 total))))
+    (format "[%d%%]" (if (and (= p 0) (> completed 0)) 1 p))))
+
 (provide 'org-macs)
 
 ;; Local variables:
diff --git a/lisp/org.el b/lisp/org.el
index c304ab9472..cb56dcdb77 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -10189,9 +10189,8 @@ statistics everywhere."
                   (outline-next-heading)))
              (setq new
                     (if is-percent
-                        (format "[%d%%]" (floor (* 100.0 cnt-done)
-                                               (max 1 cnt-all)))
-                      (format "[%d/%d]" cnt-done cnt-all))
+                        (org-format-percent-cookie cnt-done cnt-all)
+                     (format "[%d/%d]" cnt-done cnt-all))
                     ndel (- (match-end 0) checkbox-beg))
               (goto-char (match-end 0))
               (unless (string-equal new (buffer-substring checkbox-beg 
(match-end 0)))
diff --git a/testing/lisp/test-org-list.el b/testing/lisp/test-org-list.el
index a3a526ba69..5f069dee06 100644
--- a/testing/lisp/test-org-list.el
+++ b/testing/lisp/test-org-list.el
@@ -1206,7 +1206,25 @@ b. Item 2<point>"
   - [X] item2"
                   (let ((org-checkbox-hierarchical-statistics nil))
                     (org-update-checkbox-count))
-                  (buffer-string)))))
+                  (buffer-string))))
+  (let ((checklist (concat "- [%]\n"          ; 0/101 = 0%
+                           (mapconcat #'identity
+                                      (make-list 101 "  - [ ]") "\n"))))
+    (should (string-match "\\[0%\\]" (org-test-with-temp-text checklist
+                                       (org-update-checkbox-count)
+                                       (buffer-string)))))
+  (let ((checklist (concat "- [%]\n  - [X]\n" ; 1/101 = 0.99% -> 1%
+                           (mapconcat #'identity
+                                      (make-list 100 "  - [ ]") "\n"))))
+    (should (string-match "\\[1%\\]" (org-test-with-temp-text checklist
+                                       (org-update-checkbox-count)
+                                       (buffer-string)))))
+  (let ((checklist (concat "- [%]\n  - [ ]\n" ; 200/201 = 99.5% -> 99%
+                           (mapconcat #'identity
+                                      (make-list 200 "  - [X]") "\n"))))
+    (should (string-match "\\[99%\\]" (org-test-with-temp-text checklist
+                                        (org-update-checkbox-count)
+                                        (buffer-string))))))
 
 
 ;;; API
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index f885645406..e38d20d9cb 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -4238,6 +4238,63 @@ text"
       (org-ctrl-c-ctrl-c))
     (should-not org-columns-overlays)))
 
+(ert-deftest test-org/update-todo-statistics-cookies ()
+  "Test updating TODO statistics cookies."
+  (let ((N 3)
+        (parent "* [/]"))
+    (dolist (n (number-sequence 0 N))
+      (let* ((match (format "\\[%d/%d\\]" n N))
+             (done (mapconcat #'(lambda (n) (format "** DONE D%d" n))
+                              (number-sequence 1 n) "\n"))
+             (todo (mapconcat #'(lambda (n) (format "** TODO T%d" n))
+                              (number-sequence 1 (- N n)) "\n"))
+             (tree (concat parent "\n" done "\n" todo)))
+        (should (string-match match
+                              (org-test-with-temp-text tree
+                                (org-update-statistics-cookies t)
+                                (buffer-string)))))))
+  (let ((N 3)
+        (pvals '(0 33 66 100))
+        (parent "* [%]"))
+    (dolist (n (number-sequence 0 N))
+      (let* ((match (format "\\[%d%%\\]" (elt pvals n)))
+             (done (mapconcat #'(lambda (n) (format "** DONE D%d" n))
+                              (number-sequence 1 n) "\n"))
+             (todo (mapconcat #'(lambda (n) (format "** TODO T%d" n))
+                              (number-sequence 1 (- N n)) "\n"))
+             (tree (concat parent "\n" done "\n" todo)))
+        (should (string-match match
+                              (org-test-with-temp-text tree
+                                (org-update-statistics-cookies t)
+                                (buffer-string)))))))
+  (let ((N 101)
+        (parent "* [%]"))
+    (let ((match "\\[0%\\]")            ; 0/101 -> 0%
+          (tree (concat parent "\n"
+                        (mapconcat #'(lambda (n) (format "** TODO T%d" n))
+                                   (number-sequence 1 N) "\n"))))
+      (should (string-match match
+                            (org-test-with-temp-text tree
+                              (org-update-statistics-cookies t)
+                              (buffer-string)))))
+    (let ((match "\\[1%\\]")            ; 1/101 -> 0.99% -> 1%
+          (tree (concat parent "\n** DONE D1\n"
+                        (mapconcat #'(lambda (n) (format "** TODO T%d" n))
+                                   (number-sequence 1 (1- N)) "\n"))))
+      (should (string-match match
+                            (org-test-with-temp-text tree
+                              (org-update-statistics-cookies t)
+                              (buffer-string))))))
+  (let ((N 201)
+        (parent "* [%]"))
+    (let ((match "\\[99%\\]")           ; 200/201 -> 99.5% -> 99%
+          (tree (concat parent "\n** TODO T1\n"
+                        (mapconcat #'(lambda (n) (format "** DONE D%d" n))
+                                   (number-sequence 1 N) "\n"))))
+      (should (string-match match
+                            (org-test-with-temp-text tree
+                              (org-update-statistics-cookies t)
+                              (buffer-string)))))))
 
 ;;; Navigation
 

Reply via email to