1 ;;; org-datetree.el --- Create date entries in a tree -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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
57 tree can be found. If it is the sympol `subtree-at-point', then the tree
58 will be built under the headline at point."
59 (setq-local org-datetree-base-level
1)
61 (if (eq keep-restriction
'subtree-at-point
)
63 (unless (org-at-heading-p) (error "Not at heading"))
65 (org-narrow-to-subtree)
66 (setq-local org-datetree-base-level
67 (org-get-valid-level (org-current-level) 1)))
68 (unless keep-restriction
(widen))
69 ;; Support the old way of tree placement, using a property
70 (let ((prop (org-find-property "DATE_TREE")))
73 (setq-local org-datetree-base-level
74 (org-get-valid-level (org-current-level) 1))
75 (org-narrow-to-subtree))))
76 (goto-char (point-min))
77 (let ((year (calendar-extract-year d
))
78 (month (calendar-extract-month d
))
79 (day (calendar-extract-day d
)))
80 (org-datetree--find-create
81 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
82 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
84 (org-datetree--find-create
85 "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
87 (org-datetree--find-create
88 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
92 (defun org-datetree-find-iso-week-create (d &optional keep-restriction
)
93 "Find or create an ISO week entry for date D.
94 Compared to `org-datetree-find-date-create' this function creates
95 entries ordered by week instead of months.
96 When it is nil, the buffer will be widened to make sure an existing date
97 tree can be found. If it is the sympol `subtree-at-point', then the tree
98 will be built under the headline at point."
99 (setq-local org-datetree-base-level
1)
101 (if (eq keep-restriction
'subtree-at-point
)
103 (unless (org-at-heading-p) (error "Not at heading"))
105 (org-narrow-to-subtree)
106 (setq-local org-datetree-base-level
107 (org-get-valid-level (org-current-level) 1)))
108 (unless keep-restriction
(widen))
109 ;; Support the old way of tree placement, using a property
110 (let ((prop (org-find-property "WEEK_TREE")))
113 (setq-local org-datetree-base-level
114 (org-get-valid-level (org-current-level) 1))
115 (org-narrow-to-subtree))))
116 (goto-char (point-min))
118 (let* ((year (calendar-extract-year d
))
119 (month (calendar-extract-month d
))
120 (day (calendar-extract-day d
))
121 (time (encode-time 0 0 0 day month year
))
122 (iso-date (calendar-iso-from-absolute
123 (calendar-absolute-from-gregorian d
)))
124 (weekyear (nth 2 iso-date
))
125 (week (nth 0 iso-date
)))
126 ;; ISO 8601 week format is %G-W%V(-%u)
127 (org-datetree--find-create
128 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
129 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
131 (format-time-string "%G" time
))
132 (org-datetree--find-create
133 "^\\*+[ \t]+%d-W\\([0-5][0-9]\\)$"
135 (format-time-string "%G-W%V" time
))
136 ;; For the actual day we use the regular date instead of ISO week.
137 (org-datetree--find-create
138 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
141 (defun org-datetree--find-create (regex year
&optional month day insert
)
142 "Find the datetree matched by REGEX for YEAR, MONTH, or DAY.
143 REGEX is passed to `format' with YEAR, MONTH, and DAY as
144 arguments. Match group 1 is compared against the specified date
145 component. If INSERT is non-nil and there is no match then it is
146 inserted into the buffer."
148 (org-narrow-to-subtree))
149 (let ((re (format regex year month day
))
151 (goto-char (point-min))
152 (while (and (setq match
(re-search-forward re nil t
))
153 (goto-char (match-beginning 1))
154 (< (string-to-number (match-string 1)) (or day month year
))))
157 (goto-char (point-max))
158 (unless (bolp) (insert "\n"))
159 (org-datetree-insert-line year month day insert
))
160 ((= (string-to-number (match-string 1)) (or day month year
))
164 (org-datetree-insert-line year month day insert
)))))
166 (defun org-datetree-insert-line (year &optional month day text
)
167 (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point))
168 (insert "\n" (make-string org-datetree-base-level ?
*) " \n")
170 (when month
(org-do-demote))
171 (when day
(org-do-demote))
174 (insert (format "%d" year
))
178 (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year
))
179 (format-time-string "-%m %B" (encode-time 0 0 0 1 month year
))))))
180 (when (and day org-datetree-add-timestamp
)
184 (org-insert-time-stamp
185 (encode-time 0 0 0 day month year
)
187 (eq org-datetree-add-timestamp
'inactive
))))
190 (defun org-datetree-file-entry-under (txt d
)
191 "Insert a node TXT into the date tree under date D."
192 (org-datetree-find-date-create d
)
193 (let ((level (org-get-valid-level (funcall outline-level
) 1)))
194 (org-end-of-subtree t t
)
195 (org-back-over-empty-lines)
196 (org-paste-subtree level txt
)))
198 (defun org-datetree-cleanup ()
199 "Make sure all entries in the current tree are under the correct date.
200 It may be useful to restrict the buffer to the applicable portion
201 before running this command, even though the command tries to be smart."
203 (goto-char (point-min))
204 (let ((dre (concat "\\<" org-deadline-string
"\\>[ \t]*\\'"))
205 (sre (concat "\\<" org-scheduled-string
"\\>[ \t]*\\'")))
206 (while (re-search-forward org-ts-regexp nil t
)
208 (let ((tmp (buffer-substring
209 (max (line-beginning-position)
210 (- (match-beginning 0) org-ds-keyword-length
))
211 (match-beginning 0))))
212 (when (or (string-suffix-p "-" tmp
)
213 (string-match dre tmp
)
214 (string-match sre tmp
))
216 (let* ((dct (decode-time (org-time-string-to-time (match-string 0))))
217 (date (list (nth 4 dct
) (nth 3 dct
) (nth 5 dct
)))
222 (hdl-pos (progn (org-back-to-heading t
) (point))))
223 (unless (org-up-heading-safe)
224 ;; No parent, we are not in a date tree.
227 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
228 ;; Parent looks wrong, we are not in a date tree.
231 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day
))
232 ;; At correct date already, do nothing.
235 ;; OK, we need to refile this entry.
240 (org-datetree-file-entry-under (current-kill 0) date
)))))))))
242 (provide 'org-datetree
)
245 ;; generated-autoload-file: "org-loaddefs.el"
248 ;;; org-datetree.el ends here