1 ;;; org-datetree.el --- Create date entries in a tree -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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
38 DATE_TREE (or WEEK_TREE for ISO week entries) property (any
39 value), the date tree will become a subtree under that entry, so
40 the base level will be properly adjusted.")
42 (defcustom org-datetree-add-timestamp nil
43 "When non-nil, add a time stamp matching date of entry.
44 Added time stamp is active unless value is `inactive'."
48 (const :tag
"Do not add a time stamp" nil
)
49 (const :tag
"Add an inactive time stamp" inactive
)
50 (const :tag
"Add an active time stamp" active
)))
53 (defun org-datetree-find-date-create (d &optional keep-restriction
)
54 "Find or create an entry for date D.
55 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
56 When it is nil, the buffer will be widened to make sure an existing date
58 (setq-local org-datetree-base-level
1)
59 (or keep-restriction
(widen))
61 (let ((prop (org-find-property "DATE_TREE")))
64 (setq-local org-datetree-base-level
65 (org-get-valid-level (org-current-level) 1))
66 (org-narrow-to-subtree)))
67 (goto-char (point-min))
68 (let ((year (calendar-extract-year d
))
69 (month (calendar-extract-month d
))
70 (day (calendar-extract-day d
)))
71 (org-datetree--find-create
72 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
73 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
75 (org-datetree--find-create
76 "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
78 (org-datetree--find-create
79 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
83 (defun org-datetree-find-iso-week-create (d &optional keep-restriction
)
84 "Find or create an ISO week entry for date D.
85 Compared to `org-datetree-find-date-create' this function creates
86 entries ordered by week instead of months.
87 If KEEP-RESTRICTION is non-nil, do not widen the buffer. When it
88 is nil, the buffer will be widened to make sure an existing date
90 (setq-local org-datetree-base-level
1)
91 (or keep-restriction
(widen))
93 (let ((prop (org-find-property "WEEK_TREE")))
96 (setq-local org-datetree-base-level
97 (org-get-valid-level (org-current-level) 1))
98 (org-narrow-to-subtree)))
99 (goto-char (point-min))
101 (let* ((year (calendar-extract-year d
))
102 (month (calendar-extract-month d
))
103 (day (calendar-extract-day d
))
104 (time (encode-time 0 0 0 day month year
))
105 (iso-date (calendar-iso-from-absolute
106 (calendar-absolute-from-gregorian d
)))
107 (weekyear (nth 2 iso-date
))
108 (week (nth 0 iso-date
)))
109 ;; ISO 8601 week format is %G-W%V(-%u)
110 (org-datetree--find-create
111 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
112 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
114 (format-time-string "%G" time
))
115 (org-datetree--find-create
116 "^\\*+[ \t]+%d-W\\([0-5][0-9]\\)$"
118 (format-time-string "%G-W%V" time
))
119 ;; For the actual day we use the regular date instead of ISO week.
120 (org-datetree--find-create
121 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
124 (defun org-datetree--find-create (regex year
&optional month day insert
)
125 "Find the datetree matched by REGEX for YEAR, MONTH, or DAY.
126 REGEX is passed to `format' with YEAR, MONTH, and DAY as
127 arguments. Match group 1 is compared against the specified date
128 component. If INSERT is non-nil and there is no match then it is
129 inserted into the buffer."
131 (org-narrow-to-subtree))
132 (let ((re (format regex year month day
))
134 (goto-char (point-min))
135 (while (and (setq match
(re-search-forward re nil t
))
136 (goto-char (match-beginning 1))
137 (< (string-to-number (match-string 1)) (or day month year
))))
140 (goto-char (point-max))
141 (unless (bolp) (insert "\n"))
142 (org-datetree-insert-line year month day insert
))
143 ((= (string-to-number (match-string 1)) (or day month year
))
147 (org-datetree-insert-line year month day insert
)))))
149 (defun org-datetree-insert-line (year &optional month day text
)
150 (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point))
151 (insert "\n" (make-string org-datetree-base-level ?
*) " \n")
153 (when month
(org-do-demote))
154 (when day
(org-do-demote))
157 (insert (format "%d" year
))
161 (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year
))
162 (format-time-string "-%m %B" (encode-time 0 0 0 1 month year
))))))
163 (when (and day org-datetree-add-timestamp
)
167 (org-insert-time-stamp
168 (encode-time 0 0 0 day month year
)
170 (eq org-datetree-add-timestamp
'inactive
))))
173 (defun org-datetree-file-entry-under (txt d
)
174 "Insert a node TXT into the date tree under date D."
175 (org-datetree-find-date-create d
)
176 (let ((level (org-get-valid-level (funcall outline-level
) 1)))
177 (org-end-of-subtree t t
)
178 (org-back-over-empty-lines)
179 (org-paste-subtree level txt
)))
181 (defun org-datetree-cleanup ()
182 "Make sure all entries in the current tree are under the correct date.
183 It may be useful to restrict the buffer to the applicable portion
184 before running this command, even though the command tries to be smart."
186 (goto-char (point-min))
187 (let ((dre (concat "\\<" org-deadline-string
"\\>[ \t]*\\'"))
188 (sre (concat "\\<" org-scheduled-string
"\\>[ \t]*\\'")))
189 (while (re-search-forward org-ts-regexp nil t
)
191 (let ((tmp (buffer-substring
192 (max (line-beginning-position)
193 (- (match-beginning 0) org-ds-keyword-length
))
194 (match-beginning 0))))
195 (when (or (string-suffix-p "-" tmp
)
196 (string-match dre tmp
)
197 (string-match sre tmp
))
199 (let* ((dct (decode-time (org-time-string-to-time (match-string 0))))
200 (date (list (nth 4 dct
) (nth 3 dct
) (nth 5 dct
)))
205 (hdl-pos (progn (org-back-to-heading t
) (point))))
206 (unless (org-up-heading-safe)
207 ;; No parent, we are not in a date tree.
210 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
211 ;; Parent looks wrong, we are not in a date tree.
214 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day
))
215 ;; At correct date already, do nothing.
218 ;; OK, we need to refile this entry.
223 (org-datetree-file-entry-under (current-kill 0) date
)))))))))
225 (provide 'org-datetree
)
228 ;; generated-autoload-file: "org-loaddefs.el"
231 ;;; org-datetree.el ends here