浏览代码

org-capture: 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.
Jason Dunsmore 5 年之前
父节点
当前提交
a410413be8
共有 5 个文件被更改,包括 46 次插入11 次删除
  1. 4 3
      doc/org-manual.org
  2. 4 1
      etc/ORG-NEWS
  3. 4 3
      lisp/org-capture.el
  4. 23 4
      lisp/org-datetree.el
  5. 11 0
      testing/lisp/test-org-datetree.el

+ 4 - 3
doc/org-manual.org

@@ -7601,9 +7601,10 @@ Now lets look at the elements of a template definition.  Each entry in
 
 
   - ~:tree-type~ ::
   - ~: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~ ::
   - ~:unnarrowed~ ::
 
 

+ 4 - 1
etc/ORG-NEWS

@@ -392,7 +392,9 @@ the headline to use for making the table of contents.
 ,* Another section
 ,* Another section
 ,#+TOC: headlines 1 :target "#TargetSection"
 ,#+TOC: headlines 1 :target "#TargetSection"
 #+end_example
 #+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
 ** New functions
 *** ~org-dynamic-block-insert-dblock~
 *** ~org-dynamic-block-insert-dblock~
 
 
@@ -405,6 +407,7 @@ dynamic block in ~org-dynamic-block-alist~.
 *** ~org-table-cell-left~
 *** ~org-table-cell-left~
 *** ~org-table-cell-right~
 *** ~org-table-cell-right~
 *** ~org-habit-toggle-display-in-agenda~
 *** ~org-habit-toggle-display-in-agenda~
+*** ~org-datetree-find-month-create~
 ** Removed functions and variables
 ** Removed functions and variables
 *** Removed Org Drill
 *** Removed Org Drill
 
 

+ 4 - 3
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
 	   ;; Make a date/week tree entry, with the current date (or
 	   ;; yesterday, if we are extending dates for a couple of hours)
 	   ;; yesterday, if we are extending dates for a couple of hours)
 	   (funcall
 	   (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
 	    (calendar-gregorian-from-absolute
 	     (cond
 	     (cond
 	      (org-overriding-default-time
 	      (org-overriding-default-time

+ 23 - 4
lisp/org-datetree.el

@@ -51,11 +51,29 @@ Added time stamp is active unless value is `inactive'."
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-datetree-find-date-create (d &optional keep-restriction)
 (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.
 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
 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
 tree can be found.  If it is the symbol `subtree-at-point', then the tree
 will be built under the headline at point."
 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)
   (setq-local org-datetree-base-level 1)
   (save-restriction
   (save-restriction
     (if (eq keep-restriction 'subtree-at-point)
     (if (eq keep-restriction 'subtree-at-point)
@@ -84,9 +102,10 @@ will be built under the headline at point."
       (org-datetree--find-create
       (org-datetree--find-create
        "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
        "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
        year month)
        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
 ;;;###autoload
 (defun org-datetree-find-iso-week-create (d &optional keep-restriction)
 (defun org-datetree-find-iso-week-create (d &optional keep-restriction)

+ 11 - 0
testing/lisp/test-org-datetree.el

@@ -113,6 +113,17 @@
 	(org-datetree-find-date-create '(3 29 2012)))
 	(org-datetree-find-date-create '(3 29 2012)))
       (buffer-substring (point) (line-end-position))))))
       (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 ()
 (ert-deftest test-org-datetree/find-iso-week-create ()
   "Test `org-datetree-find-iso-date-create' specificaiton."
   "Test `org-datetree-find-iso-date-create' specificaiton."
   ;; When date is missing, create it.
   ;; When date is missing, create it.