emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Update statistic cookies when archiving
@ 2017-09-02 20:09 Jay Kamat
  2017-09-02 20:11 ` Jay Kamat
  0 siblings, 1 reply; 5+ messages in thread
From: Jay Kamat @ 2017-09-02 20:09 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Currently, statistic cookies do not update when archiving headings. For
example, when archiving the 'Two' header in the below example:

* Top [50%]
** TODO One
** DONE Two

The status cookie on 'Top' does not get set to [100%].

I have attached a draft patch to add support for this. However, I think
it needs a little bit more work.

There are 3 archive methods, org-archive-subtree,
org-archive-to-archive-sibling, and org-archive-set-tag.

For org-archive-set-tag, setting the :ARCHIVE: tag does not seem to
affect the status cookie, so I did not touch that.

For org-archive-to-archive-sibling, I used the
(org-update-parent-todo-statistics) function, which seems to work as
expected.

For org-archive-subtree, the solution I came up with is a little more
complicated, to handle the edge case of archiving the last child
header. In this case, 'parent' refers to the wrong (or non-existent)
header, so I use a small function to call (org-up-heading-safe) on one
header above point, which seems to work even for this edge case. I'm not
entirely sure if it's the proper solution though.

Let me know if you have any feedback!

-Jay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Update statistic cookies when archiving
  2017-09-02 20:09 [PATCH] Update statistic cookies when archiving Jay Kamat
@ 2017-09-02 20:11 ` Jay Kamat
  2017-09-03  8:02   ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Jay Kamat @ 2017-09-02 20:11 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 58 bytes --]


Sorry, forgot to actually attach the patch, here it is.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-archive.el-Update-statistic-cookies-when-archivi.patch --]
[-- Type: text/x-diff, Size: 1591 bytes --]

From 95cdfa8c3ec81b3a0763b68044611c10a4dadc29 Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygkamat@gmail.com>
Date: Sat, 2 Sep 2017 15:57:36 -0400
Subject: [PATCH] org-archive.el: Update statistic cookies when archiving

* lisp/org-archive.el (org-archive-subtree): Update todo statistics
  when calling `org-archive-subtree'.
(org-archive-to-archive-sibling): Update cookie statistics when
calling `org-archive-to-archive-sibling'.

This can be disabled by setting `org-provide-todo-statistics' to nil.
---
 lisp/org-archive.el | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lisp/org-archive.el b/lisp/org-archive.el
index adb922e75..9c24d356c 100644
--- a/lisp/org-archive.el
+++ b/lisp/org-archive.el
@@ -393,6 +393,12 @@ direct children of this heading."
 	(when (featurep 'org-inlinetask)
 	  (org-inlinetask-remove-END-maybe))
 	(setq org-markers-to-move nil)
+	(when org-provide-todo-statistics
+	  (save-excursion
+	    ;; Go to parent, even if no children exist
+	    (org-up-heading-safe)
+	    ;; Update cookie of parent
+	    (org-update-statistics-cookies nil)))
 	(message "Subtree archived %s"
 		 (if (eq this-buffer buffer)
 		     (concat "under heading: " heading)
@@ -470,6 +476,9 @@ Archiving time is retained in the ARCHIVE_TIME node property."
 	(outline-hide-subtree)
 	(org-cycle-show-empty-lines 'folded)
 	(goto-char pos)))
+    (when org-provide-todo-statistics
+      ;; update todo statistics of parent
+      (org-update-parent-todo-statistics))
     (org-reveal)
     (if (looking-at "^[ \t]*$")
 	(outline-next-visible-heading 1))))
-- 
2.11.0


[-- Attachment #3: Type: text/plain, Size: 1239 bytes --]



Jay Kamat <jaygkamat@gmail.com> writes:

> Hi,
>
> Currently, statistic cookies do not update when archiving headings. For
> example, when archiving the 'Two' header in the below example:
>
> * Top [50%]
> ** TODO One
> ** DONE Two
>
> The status cookie on 'Top' does not get set to [100%].
>
> I have attached a draft patch to add support for this. However, I think
> it needs a little bit more work.
>
> There are 3 archive methods, org-archive-subtree,
> org-archive-to-archive-sibling, and org-archive-set-tag.
>
> For org-archive-set-tag, setting the :ARCHIVE: tag does not seem to
> affect the status cookie, so I did not touch that.
>
> For org-archive-to-archive-sibling, I used the
> (org-update-parent-todo-statistics) function, which seems to work as
> expected.
>
> For org-archive-subtree, the solution I came up with is a little more
> complicated, to handle the edge case of archiving the last child
> header. In this case, 'parent' refers to the wrong (or non-existent)
> header, so I use a small function to call (org-up-heading-safe) on one
> header above point, which seems to work even for this edge case. I'm not
> entirely sure if it's the proper solution though.
>
> Let me know if you have any feedback!
>
> -Jay

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Update statistic cookies when archiving
  2017-09-02 20:11 ` Jay Kamat
@ 2017-09-03  8:02   ` Nicolas Goaziou
  2017-09-03 16:28     ` Jay Kamat
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2017-09-03  8:02 UTC (permalink / raw)
  To: Jay Kamat; +Cc: emacs-orgmode

Hello,

Jay Kamat <jaygkamat@gmail.com> writes:

> Sorry, forgot to actually attach the patch, here it is.
>
> From 95cdfa8c3ec81b3a0763b68044611c10a4dadc29 Mon Sep 17 00:00:00 2001
> From: Jay Kamat <jaygkamat@gmail.com>
> Date: Sat, 2 Sep 2017 15:57:36 -0400
> Subject: [PATCH] org-archive.el: Update statistic cookies when
> archiving

Thank you!

Be warned that we're in a feature-freeze phase. It will have to wait for
Org 9.1 before being merged.

> +	    ;; Go to parent, even if no children exist

Nitpick: Missing final dot.

> +	    (org-up-heading-safe)
> +	    ;; Update cookie of parent

Ditto.
> +	    (org-update-statistics-cookies nil)))
>  	(message "Subtree archived %s"
>  		 (if (eq this-buffer buffer)
>  		     (concat "under heading: " heading)
> @@ -470,6 +476,9 @@ Archiving time is retained in the ARCHIVE_TIME node property."
>  	(outline-hide-subtree)
>  	(org-cycle-show-empty-lines 'folded)
>  	(goto-char pos)))
> +    (when org-provide-todo-statistics
> +      ;; update todo statistics of parent

Ditto. Missing capital, too.

> +      (org-update-parent-todo-statistics))
>      (org-reveal)
>      (if (looking-at "^[ \t]*$")
>  	(outline-next-visible-heading 1))))

Could you provide some tests and an ORG-NEWS entry? For the latter, you
can start a new "Version 9.2" top heading.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Update statistic cookies when archiving
  2017-09-03  8:02   ` Nicolas Goaziou
@ 2017-09-03 16:28     ` Jay Kamat
  2017-09-06 12:32       ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Jay Kamat @ 2017-09-03 16:28 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1593 bytes --]

Hi,

> Be warned that we're in a feature-freeze phase. It will have to wait for
> Org 9.1 before being merged.

Sounds good to me!

>
>> +	    ;; Go to parent, even if no children exist
>
> Nitpick: Missing final dot.
>
>> +	    (org-up-heading-safe)
>> +	    ;; Update cookie of parent
>
> Ditto.
>> +	    (org-update-statistics-cookies nil)))
>>  	(message "Subtree archived %s"
>>  		 (if (eq this-buffer buffer)
>>  		     (concat "under heading: " heading)
>> @@ -470,6 +476,9 @@ Archiving time is retained in the ARCHIVE_TIME node property."
>>  	(outline-hide-subtree)
>>  	(org-cycle-show-empty-lines 'folded)
>>  	(goto-char pos)))
>> +    (when org-provide-todo-statistics
>> +      ;; update todo statistics of parent
>
> Ditto. Missing capital, too.
>
>> +      (org-update-parent-todo-statistics))
>>      (org-reveal)
>>      (if (looking-at "^[ \t]*$")
>>  	(outline-next-visible-heading 1))))

Fixed.

> Could you provide some tests and an ORG-NEWS entry? For the latter, you
> can start a new "Version 9.2" top heading.

Done.

I'm not sure if I did the tests 'correctly' though. I placed them in
test-org-element.el since there was a little bit of archive based
testing there, is there a better place to put them? I couldn't find any
file for org-archive testing. Maybe I'll write some more archive tests
later...

+    (should (string=
+	      (org-element-property :title (org-element-at-point))
+	      "Top [0%]")))
Is there a better way to extract the status cookie percentage from the
header? I couldn't find anything in (org-element-property)'s output.

Thanks,
-Jay


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-archive.el-Update-statistic-cookies-when-archivi.patch --]
[-- Type: text/x-diff, Size: 4293 bytes --]

From 612d4daac54e12556333fcd2e07771aa8344c86c Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygkamat@gmail.com>
Date: Sat, 2 Sep 2017 15:57:36 -0400
Subject: [PATCH] org-archive.el: Update statistic cookies when archiving

* lisp/org-archive.el (org-archive-subtree): Update todo statistics
  when calling `org-archive-subtree'.
(org-archive-to-archive-sibling): Update cookie statistics when
calling `org-archive-to-archive-sibling'.

This can be disabled by setting `org-provide-todo-statistics' to nil.
---
 etc/ORG-NEWS                     | 22 ++++++++++++++++++++++
 lisp/org-archive.el              |  9 +++++++++
 testing/lisp/test-org-element.el | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 9f3e62406..316a75f2f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -8,6 +8,28 @@ See the end of the file for license conditions.
 
 Please send Org bug reports to mailto:emacs-orgmode@gnu.org.
 
+* Version 9.2
+
+** Incompatible changes
+** New features
+*** ~org-archive~ functions update status cookies
+
+Archiving headers through ~org-archive-subtree~ and
+~org-archive-to-archive-sibling~ such as the ones listed below:
+
+#+BEGIN_SRC org
+  ,* Top [1/2]
+  ,** DONE Completed
+  ,** TODO Working
+#+END_SRC
+
+Will update the status cookie in the top level header.
+
+** Removed functions
+** Removed options
+** New functions
+** Miscellaneous
+
 * Version 9.1
 
 ** Incompatible changes
diff --git a/lisp/org-archive.el b/lisp/org-archive.el
index adb922e75..9ba73a8de 100644
--- a/lisp/org-archive.el
+++ b/lisp/org-archive.el
@@ -393,6 +393,12 @@ direct children of this heading."
 	(when (featurep 'org-inlinetask)
 	  (org-inlinetask-remove-END-maybe))
 	(setq org-markers-to-move nil)
+	(when org-provide-todo-statistics
+	  (save-excursion
+	    ;; Go to parent, even if no children exist.
+	    (org-up-heading-safe)
+	    ;; Update cookie of parent.
+	    (org-update-statistics-cookies nil)))
 	(message "Subtree archived %s"
 		 (if (eq this-buffer buffer)
 		     (concat "under heading: " heading)
@@ -470,6 +476,9 @@ Archiving time is retained in the ARCHIVE_TIME node property."
 	(outline-hide-subtree)
 	(org-cycle-show-empty-lines 'folded)
 	(goto-char pos)))
+    (when org-provide-todo-statistics
+      ;; Update todo statistics of parent.
+      (org-update-parent-todo-statistics))
     (org-reveal)
     (if (looking-at "^[ \t]*$")
 	(outline-next-visible-heading 1))))
diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index 7d1c55f36..e9506d2b0 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -1070,6 +1070,39 @@ Some other text
      (let ((org-archive-tag "Archive"))
        (org-element-property :archivedp (org-element-at-point))))))
 
+(ert-deftest test-org-element/archive-update-status-cookie ()
+  "Test archiving properly updating status cookies."
+  ;; Test org-archive-subtree with two children.
+  (org-test-with-temp-text-in-file "* Top [%]\n** DONE One\n** TODO Two"
+    (forward-line 1)
+    (org-archive-subtree)
+    (forward-line -1)
+    (should (string=
+	      (org-element-property :title (org-element-at-point))
+	      "Top [0%]")))
+  ;; Test org-archive-subtree with one child.
+  (org-test-with-temp-text-in-file "* Top [%]\n** TODO Two"
+    (forward-line 1)
+    (org-archive-subtree)
+    (forward-line -1)
+    (should (string=
+	      (org-element-property :title (org-element-at-point))
+	      "Top [100%]")))
+  ;; Test org-archive-to-archive-sibling with two children.
+  (org-test-with-temp-text "* Top [%]\n<point>** TODO One\n** DONE Two"
+    (org-archive-to-archive-sibling)
+    (forward-line -1)
+    (should (string=
+	      (org-element-property :title (org-element-at-point))
+	      "Top [100%]")))
+  ;; Test org-archive-to-archive-sibling with two children.
+  (org-test-with-temp-text "* Top [%]\n<point>** DONE Two"
+    (org-archive-to-archive-sibling)
+    (forward-line -1)
+    (should (string=
+	      (org-element-property :title (org-element-at-point))
+	      "Top [0%]"))))
+
 (ert-deftest test-org-element/headline-properties ()
   "Test properties from property drawer."
   ;; All properties from property drawer have their symbol upper
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Update statistic cookies when archiving
  2017-09-03 16:28     ` Jay Kamat
@ 2017-09-06 12:32       ` Nicolas Goaziou
  0 siblings, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2017-09-06 12:32 UTC (permalink / raw)
  To: Jay Kamat; +Cc: emacs-orgmode

Hello,

Jay Kamat <jaygkamat@gmail.com> writes:

> Done.

Applied. Thank you!
>
> I'm not sure if I did the tests 'correctly' though. I placed them in
> test-org-element.el since there was a little bit of archive based
> testing there, is there a better place to put them? I couldn't find any
> file for org-archive testing. Maybe I'll write some more archive tests
> later...

I created test-org-archive.el on your behalf.

Regards,

-- 
Nicolas Goaziou                                                0x80A93738

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-09-06 12:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-02 20:09 [PATCH] Update statistic cookies when archiving Jay Kamat
2017-09-02 20:11 ` Jay Kamat
2017-09-03  8:02   ` Nicolas Goaziou
2017-09-03 16:28     ` Jay Kamat
2017-09-06 12:32       ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).