1 ;;; org-datetree.el --- Create date entries in a tree
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains code to create entries in a tree where the top-level
28 ;; nodes represent years, the level 2 nodes represent the months, and the
29 ;; level 1 entries days.
35 (defvar org-datetree-base-level
1
36 "The level at which years should be placed in the date tree.
37 This is normally one, but if the buffer has an entry with a DATE_TREE
38 property (any value), the date tree will become a subtree under that entry,
39 so the base level will be properly adjusted.")
41 (defcustom org-datetree-add-timestamp nil
42 "When non-nil, add a time stamp when create a datetree entry."
46 (const :tag
"Do not add a time stamp" nil
)
47 (const :tag
"Add an inactive time stamp" inactive
)
48 (const :tag
"Add an active time stamp" active
)))
51 (defun org-datetree-find-date-create (date &optional keep-restriction
)
52 "Find or create an entry for DATE.
53 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
54 When it is nil, the buffer will be widened to make sure an existing date
56 (let ((year (nth 2 date
))
59 (org-set-local 'org-datetree-base-level
1)
60 (or keep-restriction
(widen))
61 (goto-char (point-min))
63 (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t
)
64 (org-back-to-heading t
)
65 (org-set-local 'org-datetree-base-level
66 (org-get-valid-level (funcall outline-level
) 1))
67 (org-narrow-to-subtree))
68 (goto-char (point-min))
69 (org-datetree-find-year-create year
)
70 (org-datetree-find-month-create year month
)
71 (org-datetree-find-day-create year month day
)
72 (goto-char (prog1 (point) (widen))))))
74 (defun org-datetree-find-year-create (year)
75 (let ((re "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:\\)?\\s-*$\\)")
77 (goto-char (point-min))
78 (while (and (setq match
(re-search-forward re nil t
))
79 (goto-char (match-beginning 1))
80 (< (string-to-number (match-string 1)) year
)))
83 (goto-char (point-max))
85 (org-datetree-insert-line year
))
86 ((= (string-to-number (match-string 1)) year
)
87 (goto-char (point-at-bol)))
90 (org-datetree-insert-line year
)))))
92 (defun org-datetree-find-month-create (year month
)
93 (org-narrow-to-subtree)
94 (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year
))
96 (goto-char (point-min))
97 (while (and (setq match
(re-search-forward re nil t
))
98 (goto-char (match-beginning 1))
99 (< (string-to-number (match-string 1)) month
)))
102 (goto-char (point-max))
103 (or (bolp) (newline))
104 (org-datetree-insert-line year month
))
105 ((= (string-to-number (match-string 1)) month
)
106 (goto-char (point-at-bol)))
108 (beginning-of-line 1)
109 (org-datetree-insert-line year month
)))))
111 (defun org-datetree-find-day-create (year month day
)
112 (org-narrow-to-subtree)
113 (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month
))
115 (goto-char (point-min))
116 (while (and (setq match
(re-search-forward re nil t
))
117 (goto-char (match-beginning 1))
118 (< (string-to-number (match-string 1)) day
)))
121 (goto-char (point-max))
122 (or (bolp) (newline))
123 (org-datetree-insert-line year month day
))
124 ((= (string-to-number (match-string 1)) day
)
125 (goto-char (point-at-bol)))
127 (beginning-of-line 1)
128 (org-datetree-insert-line year month day
)))))
130 (defun org-datetree-insert-line (year &optional month day
)
131 (let ((pos (point)) ts-type
)
132 (skip-chars-backward " \t\n")
133 (delete-region (point) pos
)
134 (insert "\n" (make-string org-datetree-base-level ?
*) " \n")
136 (if month
(org-do-demote))
137 (if day
(org-do-demote))
138 (insert (format "%d" year
))
140 (insert (format "-%02d" month
))
142 (insert (format "-%02d %s"
143 day
(format-time-string
144 "%A" (encode-time 0 0 0 day month year
))))
145 (insert (format " %s"
147 "%B" (encode-time 0 0 0 1 month year
))))))
148 (when (and day
(setq ts-type org-datetree-add-timestamp
))
151 (org-insert-time-stamp (encode-time 0 0 0 day month year
) nil ts-type
))
152 (beginning-of-line 1)))
154 (defun org-datetree-file-entry-under (txt date
)
155 "Insert a node TXT into the date tree under DATE."
156 (org-datetree-find-date-create date
)
157 (let ((level (org-get-valid-level (funcall outline-level
) 1)))
158 (org-end-of-subtree t t
)
159 (org-back-over-empty-lines)
160 (org-paste-subtree level txt
)))
162 (defun org-datetree-cleanup ()
163 "Make sure all entries in the current tree are under the correct date.
164 It may be useful to restrict the buffer to the applicable portion
165 before running this command, even though the command tries to be smart."
167 (goto-char (point-min))
168 (let ((dre (concat "\\<" org-deadline-string
"\\>[ \t]*\\'"))
169 (sre (concat "\\<" org-scheduled-string
"\\>[ \t]*\\'"))
170 dct ts tmp date year month day pos hdl-pos
)
171 (while (re-search-forward org-ts-regexp nil t
)
173 (setq ts
(match-string 0))
174 (setq tmp
(buffer-substring
175 (max (point-at-bol) (- (match-beginning 0)
176 org-ds-keyword-length
))
177 (match-beginning 0)))
178 (if (or (string-match "-\\'" tmp
)
179 (string-match dre tmp
)
180 (string-match sre tmp
))
182 (setq dct
(decode-time (org-time-string-to-time (match-string 0)))
183 date
(list (nth 4 dct
) (nth 3 dct
) (nth 5 dct
))
188 (org-back-to-heading t
)
189 (setq hdl-pos
(point))
190 (unless (org-up-heading-safe)
191 ;; No parent, we are not in a date tree
194 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
195 ;; Parent looks wrong, we are not in a date tree
198 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day
))
199 ;; At correct date already, do nothing
200 (progn (goto-char pos
) (throw 'next nil
)))
201 ;; OK, we need to refile this entry
206 (org-datetree-file-entry-under (current-kill 0) date
)))))))
208 (provide 'org-datetree
)
211 ;; generated-autoload-file: "org-loaddefs.el"
214 ;;; org-datetree.el ends here