all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Rüdiger Sonderfeld" <ruediger@c-plusplus.net>
To: emacs-orgmode@gnu.org
Cc: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Subject: [PATCH v2 1/3] org-datetree.el: Code cleanup.
Date: Mon, 07 Sep 2015 23:24:18 +0100	[thread overview]
Message-ID: <3379166.PNnx3DpU9K@descartes> (raw)
In-Reply-To: <87fv2w82tf.fsf@nicolasgoaziou.fr>

* lisp/org-datetree.el (org-datetree--find-create): New function.
(org-datetree-find-year-create, org-datetree-find-month-create,
org-datetree-find-day-create): Removed functions
(org-datetree-find-date-create): Use `org-datetree--find-create' instead
of removed functions.  Use calendar extract functions.
(org-datetree-insert-line): Do more formatting in `format-time-string'
since we call it anyway
* testing/lisp/test-org-datetree.el (test-org-datetree/find-date-create):
  Test if new entries are put at the right place.
---
 lisp/org-datetree.el              | 90 +++++++++++++--------------------------
 testing/lisp/test-org-datetree.el |  9 ++++
 2 files changed, 38 insertions(+), 61 deletions(-)

diff --git a/lisp/org-datetree.el b/lisp/org-datetree.el
index a97a9d0..ea7afe7 100644
--- a/lisp/org-datetree.el
+++ b/lisp/org-datetree.el
@@ -64,70 +64,42 @@ (defun org-datetree-find-date-create (date &optional keep-restriction)
 		       (org-get-valid-level (org-current-level) 1))
 	(org-narrow-to-subtree)))
     (goto-char (point-min))
-    (let ((year (nth 2 date))
-	  (month (car date))
-	  (day (nth 1 date)))
-      (org-datetree-find-year-create year)
-      (org-datetree-find-month-create year month)
-      (org-datetree-find-day-create year month day))))
-
-(defun org-datetree-find-year-create (year)
-  "Find the YEAR datetree or create it."
-  (let ((re "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\\([ \t]:[[:alnum:]:_@#%]+:\\)?\\s-*$\\)")
+    (let ((year (calendar-extract-year date))
+	  (month (calendar-extract-month date))
+	  (day (calendar-extract-day date)))
+      (org-datetree--find-create
+       "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
+\\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
+       year)
+      (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))))
+
+(defun org-datetree--find-create (regex year &optional month day)
+  "Find the datetree matched by REGEX for YEAR, MONTH, or DAY.
+REGEX is passed to `format' with YEAR, MONTH, and DAY as
+arguments.  Match group 1 is compared against the specified date
+component."
+  (when (or month day)
+    (org-narrow-to-subtree))
+  (let ((re (format regex year month day))
 	match)
     (goto-char (point-min))
     (while (and (setq match (re-search-forward re nil t))
 		(goto-char (match-beginning 1))
-		(< (string-to-number (match-string 1)) year)))
+		(< (string-to-number (match-string 1)) (or day month year))))
     (cond
      ((not match)
       (goto-char (point-max))
-      (or (bolp) (newline))
-      (org-datetree-insert-line year))
-     ((= (string-to-number (match-string 1)) year)
-      (goto-char (point-at-bol)))
-     (t
-      (beginning-of-line 1)
-      (org-datetree-insert-line year)))))
-
-(defun org-datetree-find-month-create (year month)
-  "Find the datetree for YEAR and MONTH or create it."
-  (org-narrow-to-subtree)
-  (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year))
-	match)
-    (goto-char (point-min))
-    (while (and (setq match (re-search-forward re nil t))
-		(goto-char (match-beginning 1))
-		(< (string-to-number (match-string 1)) month)))
-    (cond
-     ((not match)
-      (goto-char (point-max))
-      (or (bolp) (newline))
-      (org-datetree-insert-line year month))
-     ((= (string-to-number (match-string 1)) month)
-      (goto-char (point-at-bol)))
-     (t
-      (beginning-of-line 1)
-      (org-datetree-insert-line year month)))))
-
-(defun org-datetree-find-day-create (year month day)
-  "Find the datetree for YEAR, MONTH and DAY or create it."
-  (org-narrow-to-subtree)
-  (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month))
-	match)
-    (goto-char (point-min))
-    (while (and (setq match (re-search-forward re nil t))
-		(goto-char (match-beginning 1))
-		(< (string-to-number (match-string 1)) day)))
-    (cond
-     ((not match)
-      (goto-char (point-max))
-      (or (bolp) (newline))
+      (unless (bolp) (insert "\n"))
       (org-datetree-insert-line year month day))
-     ((= (string-to-number (match-string 1)) day)
-      (goto-char (point-at-bol)))
+     ((= (string-to-number (match-string 1)) (or day month year))
+      (beginning-of-line))
      (t
-      (beginning-of-line 1)
+      (beginning-of-line)
       (org-datetree-insert-line year month day)))))
 
 (defun org-datetree-insert-line (year &optional month day)
@@ -139,13 +111,9 @@ (defun org-datetree-insert-line (year &optional month day)
   (insert (format "%d" year))
   (when month
     (insert
-     (format "-%02d" month)
      (if day
-	 (format "-%02d %s"
-		 day
-		 (format-time-string "%A" (encode-time 0 0 0 day month year)))
-       (format " %s"
-	       (format-time-string "%B" (encode-time 0 0 0 1 month year))))))
+	 (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year))
+       (format-time-string "-%m %B" (encode-time 0 0 0 1 month year)))))
   (when (and day org-datetree-add-timestamp)
     (save-excursion
       (insert "\n")
diff --git a/testing/lisp/test-org-datetree.el b/testing/lisp/test-org-datetree.el
index d500130..bbc8e14 100644
--- a/testing/lisp/test-org-datetree.el
+++ b/testing/lisp/test-org-datetree.el
@@ -55,6 +55,15 @@ (ert-deftest test-org-datetree/find-date-create ()
       (let ((org-datetree-add-timestamp nil))
 	(org-datetree-find-date-create '(3 29 2012)))
       (org-trim (buffer-string)))))
+  ;; Sort new entry in right place.
+  (should
+   (string-match
+    "\\`\\* 2012\n\\*\\* 2012-02 .*\n\\*\\*\\* 2012-02-01 .*\n\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
+    (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
+      (let ((org-datetree-add-timestamp nil))
+	(org-datetree-find-date-create '(3 29 2012))
+	(org-datetree-find-date-create '(2 1 2012)))
+      (org-trim (buffer-string)))))
   ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
   ;; in entry.  When set to `inactive', insert an inactive one.
   (should
-- 
2.5.1

  reply	other threads:[~2015-09-07 22:24 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1441180730.git.ruediger@c-plusplus.net>
2015-09-02  8:06 ` [PATCH 1/3] org-datetree.el: Code cleanup Rüdiger Sonderfeld
2015-09-02 19:48   ` Nicolas Goaziou
2015-09-02  8:06 ` [PATCH 2/3] org-datetree.el: Add support for ISO week trees Rüdiger Sonderfeld
2015-09-02 19:58   ` Nicolas Goaziou
2015-09-03  0:14     ` Rüdiger Sonderfeld
2015-09-03  5:55       ` Nicolas Goaziou
2015-09-07 22:24         ` Rüdiger Sonderfeld [this message]
2015-09-07 22:24         ` [PATCH v2 " Rüdiger Sonderfeld
2015-09-07 22:24         ` [PATCH v2 3/3] org-capture.el: Add support for " Rüdiger Sonderfeld
2015-09-07 22:27         ` [PATCH 2/3] org-datetree.el: Add support for ISO " Rüdiger Sonderfeld
2015-09-08 15:53           ` Nicolas Goaziou
2015-12-29 17:48             ` [PATCH v3 1/3] org-datetree.el: Code cleanup Rüdiger Sonderfeld
2015-12-29 20:58               ` Nicolas Goaziou
2015-12-29 17:49             ` [PATCH v3 2/3] org-datetree.el: Add support for ISO week trees Rüdiger Sonderfeld
2015-12-29 20:59               ` Nicolas Goaziou
2015-12-29 17:49             ` [PATCH v3 3/3] org-capture.el: Add support for " Rüdiger Sonderfeld
2015-12-29 20:59               ` Nicolas Goaziou
2015-09-02  8:06 ` [PATCH " Rüdiger Sonderfeld
2015-09-02 19:59   ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3379166.PNnx3DpU9K@descartes \
    --to=ruediger@c-plusplus.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.