emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Add support for month trees
@ 2019-11-07 14:29 Jason
  2019-11-26  9:22 ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Jason @ 2019-11-07 14:29 UTC (permalink / raw)
  To: Org Mode


[-- Attachment #1.1: Type: text/plain, Size: 417 bytes --]

Hi,

Please find my attached patch which implements a way to capture entries
grouped by month.

I was searching for this option, but only found a way to group entries by
week or day.

I found the following posts where other people also need this feature:
https://emacs.stackexchange.com/questions/48414/monthly-date-tree
https://lists.gnu.org/archive/html/emacs-orgmode/2018-02/msg00092.html

Regards,
Jason Dunsmore

[-- Attachment #1.2: Type: text/html, Size: 738 bytes --]

[-- Attachment #2: 0001-org-capture.el-Add-support-for-month-trees.patch --]
[-- Type: text/x-patch, Size: 6353 bytes --]

From 9858c2acc3aef0561dacd740d2ee353f77239fe2 Mon Sep 17 00:00:00 2001
From: Jason Dunsmore <jasondunsmore@gmail.com>
Date: Wed, 6 Nov 2019 21:49:43 -0600
Subject: [PATCH] org-capture.el: Add support for month trees

* doc/org-manual.org: Add `:tree-type month' option for capture
  templates.
* etc/ORG-NEWS: Document new `:tree-type month' option.
* lisp/org-capture.el (org-capture-set-target-location): Add
  `:tree-type month' option to capture templates to group entries by
  month.
* lisp/org-datetree.el (org-datetree-find-month-create): Add
  `org-datetree-find-month-create' function to add datetree entries
  grouped by month.
* testing/lisp/test-org-datetree.el
  (test-org-datetree/find-month-create): Add test for new function.
---
 doc/org-manual.org                |  7 ++++---
 etc/ORG-NEWS                      |  5 ++++-
 lisp/org-capture.el               | 10 +++++++---
 lisp/org-datetree.el              | 27 +++++++++++++++++++++++----
 testing/lisp/test-org-datetree.el | 11 +++++++++++
 5 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 5e69ef074..cd133739d 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -7592,9 +7592,10 @@ Now lets look at the elements of a template definition.  Each entry in
 
   - ~:tree-type~ ::
 
-    When ~week~, make a week tree instead of the month tree, i.e.,
-    place the headings for each day under a heading with the current
-    ISO week.
+    Use ~week~ to make a week tree instead of the month-day tree,
+    i.e., place the headings for each day under a heading with the
+    current ISO week. Use @code{month} to group entries by month
+    only. Default is to group entries by day.
 
   - ~:unnarrowed~ ::
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index bdcfc24fd..35e8d2864 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -335,7 +335,9 @@ the headline to use for making the table of contents.
 ,* Another section
 ,#+TOC: headlines 1 :target "#TargetSection"
 #+end_example
-
+*** New option to group captured datetime entries by month
+A new `:tree-type month' option was added to org-capture-templates to
+group new datetime entries by month.
 ** New functions
 *** ~org-dynamic-block-insert-dblock~
 
@@ -348,6 +350,7 @@ dynamic block in ~org-dynamic-block-alist~.
 *** ~org-table-cell-left~
 *** ~org-table-cell-right~
 *** ~org-habit-toggle-display-in-agenda~
+*** ~org-datetree-find-month-create~
 ** Removed functions and variables
 *** Removed Org Drill
 
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 4f97e17ea..ab0db0c27 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -998,9 +998,13 @@ Store them in the capture property list."
 	   ;; Make a date/week tree entry, with the current date (or
 	   ;; yesterday, if we are extending dates for a couple of hours)
 	   (funcall
-	    (if (eq (org-capture-get :tree-type) 'week)
-		#'org-datetree-find-iso-week-create
-	      #'org-datetree-find-date-create)
+	    (cond
+	     ((eq (org-capture-get :tree-type) 'week)
+	      #'org-datetree-find-iso-week-create)
+	     ((eq (org-capture-get :tree-type) 'month)
+	      #'org-datetree-find-month-create)
+	     (t
+	      #'org-datetree-find-date-create))
 	    (calendar-gregorian-from-absolute
 	     (cond
 	      (org-overriding-default-time
diff --git a/lisp/org-datetree.el b/lisp/org-datetree.el
index b4797de1e..0ae3cf6ec 100644
--- a/lisp/org-datetree.el
+++ b/lisp/org-datetree.el
@@ -51,11 +51,29 @@ Added time stamp is active unless value is `inactive'."
 
 ;;;###autoload
 (defun org-datetree-find-date-create (d &optional keep-restriction)
-  "Find or create an entry for date D.
+  "Find or create a day entry for date D.
 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
 When it is nil, the buffer will be widened to make sure an existing date
 tree can be found.  If it is the symbol `subtree-at-point', then the tree
 will be built under the headline at point."
+  (org-datetree--find-create-group d 'day keep-restriction))
+
+;;;###autoload
+(defun org-datetree-find-month-create (d &optional keep-restriction)
+  "Find or create a month entry for date D.
+Compared to `org-datetree-find-date-create' this function creates
+entries grouped by month instead of days.
+If KEEP-RESTRICTION is non-nil, do not widen the buffer.
+When it is nil, the buffer will be widened to make sure an existing date
+tree can be found.  If it is the symbol `subtree-at-point', then the tree
+will be built under the headline at point."
+  (org-datetree--find-create-group d 'month keep-restriction))
+
+(defun org-datetree--find-create-group
+    (d time-grouping &optional keep-restriction)
+  "Find or create an entry for date D.
+If time-period is day, group entries by day. If time-period is
+month, then group entries by month."
   (setq-local org-datetree-base-level 1)
   (save-restriction
     (if (eq keep-restriction 'subtree-at-point)
@@ -84,9 +102,10 @@ will be built under the headline at point."
       (org-datetree--find-create
        "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
        year month)
-      (org-datetree--find-create
-       "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
-       year month day))))
+      (if (eq time-grouping 'day)
+	  (org-datetree--find-create
+	   "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
+	   year month day)))))
 
 ;;;###autoload
 (defun org-datetree-find-iso-week-create (d &optional keep-restriction)
diff --git a/testing/lisp/test-org-datetree.el b/testing/lisp/test-org-datetree.el
index 5d55f6fc6..5557d5e23 100644
--- a/testing/lisp/test-org-datetree.el
+++ b/testing/lisp/test-org-datetree.el
@@ -113,6 +113,17 @@
 	(org-datetree-find-date-create '(3 29 2012)))
       (buffer-substring (point) (line-end-position))))))
 
+(ert-deftest test-org-datetree/find-month-create ()
+  "Test `org-datetree-find-month-create' specifications."
+  ;; When date is missing, create it with the entry under month.
+  (should
+   (string-match
+    "\\`\\* 2012\n\\*\\* 2012-03 .*\\'"
+    (org-test-with-temp-text ""
+      (let ((org-datetree-add-timestamp nil))
+	(org-datetree-find-month-create '(3 29 2012)))
+      (org-trim (buffer-string))))))
+
 (ert-deftest test-org-datetree/find-iso-week-create ()
   "Test `org-datetree-find-iso-date-create' specificaiton."
   ;; When date is missing, create it.
-- 
2.17.1


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

* Re: [PATCH] Add support for month trees
  2019-11-07 14:29 [PATCH] Add support for month trees Jason
@ 2019-11-26  9:22 ` Nicolas Goaziou
  2019-11-27  0:14   ` Jason Dunsmore
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2019-11-26  9:22 UTC (permalink / raw)
  To: Jason; +Cc: Org Mode

Hello,

Jason <jasondunsmore@gmail.com> writes:

> Please find my attached patch which implements a way to capture entries
> grouped by month.

Thank you.

> I was searching for this option, but only found a way to group entries by
> week or day.
>
> I found the following posts where other people also need this feature:
> https://emacs.stackexchange.com/questions/48414/monthly-date-tree
> https://lists.gnu.org/archive/html/emacs-orgmode/2018-02/msg00092.html

I think this can be tested in "next" branch. Could you rebase your patch
against it?

> * etc/ORG-NEWS: Document new `:tree-type month' option.

No need to provide this information in the commit message, IMO. Of
course, it doesn't hurt in any case. Just sayin'.

> +    Use ~week~ to make a week tree instead of the month-day tree,
> +    i.e., place the headings for each day under a heading with the
> +    current ISO week. Use @code{month} to group entries by month
> +    only. Default is to group entries by day.

You need to put two spaces after full stops.

> +	    (cond
> +	     ((eq (org-capture-get :tree-type) 'week)
> +	      #'org-datetree-find-iso-week-create)
> +	     ((eq (org-capture-get :tree-type) 'month)
> +	      #'org-datetree-find-month-create)
> +	     (t
> +	      #'org-datetree-find-date-create))

Could you refactor that with `case' instead? I.e.,

    (case (org-capture-get :tree-type)
     (`week ...)
     ...)
> +      (if (eq time-grouping 'day)
> +	  (org-datetree--find-create
> +	   "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
> +	   year month day)))))

Please use `when' (or `unless') instead of one-armed `if'.

Could you send an updated patch?

Regards,

-- 
Nicolas Goaziou

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

* Re: [PATCH] Add support for month trees
  2019-11-26  9:22 ` Nicolas Goaziou
@ 2019-11-27  0:14   ` Jason Dunsmore
  2019-12-09 19:10     ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Dunsmore @ 2019-11-27  0:14 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: Org Mode


[-- Attachment #1.1: Type: text/plain, Size: 2001 bytes --]

Hi Nicolas,

I've addressed each of your points in my updated patch (attached).

Thanks,
Jason

On Tue, Nov 26, 2019 at 3:22 AM Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:

> Hello,
>
> Jason <jasondunsmore@gmail.com> writes:
>
> > Please find my attached patch which implements a way to capture entries
> > grouped by month.
>
> Thank you.
>
> > I was searching for this option, but only found a way to group entries by
> > week or day.
> >
> > I found the following posts where other people also need this feature:
> > https://emacs.stackexchange.com/questions/48414/monthly-date-tree
> > https://lists.gnu.org/archive/html/emacs-orgmode/2018-02/msg00092.html
>
> I think this can be tested in "next" branch. Could you rebase your patch
> against it?
>
> > * etc/ORG-NEWS: Document new `:tree-type month' option.
>
> No need to provide this information in the commit message, IMO. Of
> course, it doesn't hurt in any case. Just sayin'.
>
> > +    Use ~week~ to make a week tree instead of the month-day tree,
> > +    i.e., place the headings for each day under a heading with the
> > +    current ISO week. Use @code{month} to group entries by month
> > +    only. Default is to group entries by day.
>
> You need to put two spaces after full stops.
>
> > +         (cond
> > +          ((eq (org-capture-get :tree-type) 'week)
> > +           #'org-datetree-find-iso-week-create)
> > +          ((eq (org-capture-get :tree-type) 'month)
> > +           #'org-datetree-find-month-create)
> > +          (t
> > +           #'org-datetree-find-date-create))
>
> Could you refactor that with `case' instead? I.e.,
>
>     (case (org-capture-get :tree-type)
>      (`week ...)
>      ...)
> > +      (if (eq time-grouping 'day)
> > +       (org-datetree--find-create
> > +        "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
> > +        year month day)))))
>
> Please use `when' (or `unless') instead of one-armed `if'.
>
> Could you send an updated patch?
>
> Regards,
>
> --
> Nicolas Goaziou
>

[-- Attachment #1.2: Type: text/html, Size: 3064 bytes --]

[-- Attachment #2: 0001-org-capture.el-Add-support-for-month-trees.patch --]
[-- Type: text/x-patch, Size: 6233 bytes --]

From b432ba4edb1fccf53a1a6f187e710b825b054673 Mon Sep 17 00:00:00 2001
From: Jason Dunsmore <jasondunsmore@gmail.com>
Date: Wed, 6 Nov 2019 21:49:43 -0600
Subject: [PATCH] org-capture.el: Add support for month trees

* doc/org-manual.org: Add `:tree-type month' option for capture
  templates.
* lisp/org-capture.el (org-capture-set-target-location): Add
  `:tree-type month' option to capture templates to group entries by
  month.
* lisp/org-datetree.el (org-datetree-find-month-create): Add
  `org-datetree-find-month-create' function to add datetree entries
  grouped by month.
* testing/lisp/test-org-datetree.el
  (test-org-datetree/find-month-create): Add test for new function.
---
 doc/org-manual.org                |  7 ++++---
 etc/ORG-NEWS                      |  5 ++++-
 lisp/org-capture.el               |  7 ++++---
 lisp/org-datetree.el              | 27 +++++++++++++++++++++++----
 testing/lisp/test-org-datetree.el | 11 +++++++++++
 5 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index ee7092d14..8c310a0f9 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -7599,9 +7599,10 @@ Now lets look at the elements of a template definition.  Each entry in
 
   - ~:tree-type~ ::
 
-    When ~week~, make a week tree instead of the month tree, i.e.,
-    place the headings for each day under a heading with the current
-    ISO week.
+    Use ~week~ to make a week tree instead of the month-day tree,
+    i.e., place the headings for each day under a heading with the
+    current ISO week.  Use @code{month} to group entries by month
+    only.  Default is to group entries by day.
 
   - ~:unnarrowed~ ::
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 689a07871..0b97948e6 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -345,7 +345,9 @@ the headline to use for making the table of contents.
 ,* Another section
 ,#+TOC: headlines 1 :target "#TargetSection"
 #+end_example
-
+*** New option to group captured datetime entries by month
+A new `:tree-type month' option was added to org-capture-templates to
+group new datetime entries by month.
 ** New functions
 *** ~org-dynamic-block-insert-dblock~
 
@@ -358,6 +360,7 @@ dynamic block in ~org-dynamic-block-alist~.
 *** ~org-table-cell-left~
 *** ~org-table-cell-right~
 *** ~org-habit-toggle-display-in-agenda~
+*** ~org-datetree-find-month-create~
 ** Removed functions and variables
 *** Removed Org Drill
 
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 67343e02a..39121d1fb 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -998,9 +998,10 @@ Store them in the capture property list."
 	   ;; Make a date/week tree entry, with the current date (or
 	   ;; yesterday, if we are extending dates for a couple of hours)
 	   (funcall
-	    (if (eq (org-capture-get :tree-type) 'week)
-		#'org-datetree-find-iso-week-create
-	      #'org-datetree-find-date-create)
+	    (pcase (org-capture-get :tree-type)
+	      ('week #'org-datetree-find-iso-week-create)
+	      ('month #'org-datetree-find-month-create)
+	      (t #'org-datetree-find-date-create))
 	    (calendar-gregorian-from-absolute
 	     (cond
 	      (org-overriding-default-time
diff --git a/lisp/org-datetree.el b/lisp/org-datetree.el
index b4797de1e..ee8672d7b 100644
--- a/lisp/org-datetree.el
+++ b/lisp/org-datetree.el
@@ -51,11 +51,29 @@ Added time stamp is active unless value is `inactive'."
 
 ;;;###autoload
 (defun org-datetree-find-date-create (d &optional keep-restriction)
-  "Find or create an entry for date D.
+  "Find or create a day entry for date D.
 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
 When it is nil, the buffer will be widened to make sure an existing date
 tree can be found.  If it is the symbol `subtree-at-point', then the tree
 will be built under the headline at point."
+  (org-datetree--find-create-group d 'day keep-restriction))
+
+;;;###autoload
+(defun org-datetree-find-month-create (d &optional keep-restriction)
+  "Find or create a month entry for date D.
+Compared to `org-datetree-find-date-create' this function creates
+entries grouped by month instead of days.
+If KEEP-RESTRICTION is non-nil, do not widen the buffer.
+When it is nil, the buffer will be widened to make sure an existing date
+tree can be found.  If it is the symbol `subtree-at-point', then the tree
+will be built under the headline at point."
+  (org-datetree--find-create-group d 'month keep-restriction))
+
+(defun org-datetree--find-create-group
+    (d time-grouping &optional keep-restriction)
+  "Find or create an entry for date D.
+If time-period is day, group entries by day. If time-period is
+month, then group entries by month."
   (setq-local org-datetree-base-level 1)
   (save-restriction
     (if (eq keep-restriction 'subtree-at-point)
@@ -84,9 +102,10 @@ will be built under the headline at point."
       (org-datetree--find-create
        "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
        year month)
-      (org-datetree--find-create
-       "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
-       year month day))))
+      (when (eq time-grouping 'day)
+	(org-datetree--find-create
+	 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
+	 year month day)))))
 
 ;;;###autoload
 (defun org-datetree-find-iso-week-create (d &optional keep-restriction)
diff --git a/testing/lisp/test-org-datetree.el b/testing/lisp/test-org-datetree.el
index 5d55f6fc6..5557d5e23 100644
--- a/testing/lisp/test-org-datetree.el
+++ b/testing/lisp/test-org-datetree.el
@@ -113,6 +113,17 @@
 	(org-datetree-find-date-create '(3 29 2012)))
       (buffer-substring (point) (line-end-position))))))
 
+(ert-deftest test-org-datetree/find-month-create ()
+  "Test `org-datetree-find-month-create' specifications."
+  ;; When date is missing, create it with the entry under month.
+  (should
+   (string-match
+    "\\`\\* 2012\n\\*\\* 2012-03 .*\\'"
+    (org-test-with-temp-text ""
+      (let ((org-datetree-add-timestamp nil))
+	(org-datetree-find-month-create '(3 29 2012)))
+      (org-trim (buffer-string))))))
+
 (ert-deftest test-org-datetree/find-iso-week-create ()
   "Test `org-datetree-find-iso-date-create' specificaiton."
   ;; When date is missing, create it.
-- 
2.17.1


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

* Re: [PATCH] Add support for month trees
  2019-11-27  0:14   ` Jason Dunsmore
@ 2019-12-09 19:10     ` Nicolas Goaziou
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2019-12-09 19:10 UTC (permalink / raw)
  To: Jason Dunsmore; +Cc: Org Mode

Hello,

Jason Dunsmore <jasondunsmore@gmail.com> writes:


> I've addressed each of your points in my updated patch (attached).

Applied. Thank you.

I added you to the list of contributors who have not signed FSF papers.
Let me know if you did.

Regards,

-- 
Nicolas Goaziou

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

end of thread, other threads:[~2019-12-09 19:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-11-07 14:29 [PATCH] Add support for month trees Jason
2019-11-26  9:22 ` Nicolas Goaziou
2019-11-27  0:14   ` Jason Dunsmore
2019-12-09 19:10     ` 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).