Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-datetree.el
blob2a921bfc2549d21aff175f19f20b714940a5cec0
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
8 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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.
31 ;;; Code:
33 (require 'org)
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'."
45 :group 'org-capture
46 :version "24.3"
47 :type '(choice
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)))
52 ;;;###autoload
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."
58 (setq-local org-datetree-base-level 1)
59 (or keep-restriction (widen))
60 (save-restriction
61 (let ((prop (org-find-property "DATE_TREE")))
62 (when prop
63 (goto-char prop)
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-*$\\)"
74 year)
75 (org-datetree--find-create
76 "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
77 year month)
78 (org-datetree--find-create
79 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
80 year month day))))
82 ;;;###autoload
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
89 tree can be found."
90 (setq-local org-datetree-base-level 1)
91 (or keep-restriction (widen))
92 (save-restriction
93 (let ((prop (org-find-property "WEEK_TREE")))
94 (when prop
95 (goto-char prop)
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))
100 (require 'cal-iso)
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-*$\\)"
113 weekyear nil nil
114 (format-time-string "%G" time))
115 (org-datetree--find-create
116 "^\\*+[ \t]+%d-W\\([0-5][0-9]\\)$"
117 weekyear week nil
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+$"
122 year month day))))
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."
130 (when (or month day)
131 (org-narrow-to-subtree))
132 (let ((re (format regex year month day))
133 match)
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))))
138 (cond
139 ((not match)
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))
144 (beginning-of-line))
146 (beginning-of-line)
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")
152 (backward-char)
153 (when month (org-do-demote))
154 (when day (org-do-demote))
155 (if text
156 (insert text)
157 (insert (format "%d" year))
158 (when month
159 (insert
160 (if day
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)
164 (save-excursion
165 (insert "\n")
166 (org-indent-line)
167 (org-insert-time-stamp
168 (encode-time 0 0 0 day month year)
170 (eq org-datetree-add-timestamp 'inactive))))
171 (beginning-of-line))
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."
185 (interactive)
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)
190 (catch 'next
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))
198 (throw 'next nil))
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)))
201 (year (nth 2 date))
202 (month (car date))
203 (day (nth 1 date))
204 (pos (point))
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.
208 (goto-char pos)
209 (throw 'next nil))
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.
212 (goto-char pos)
213 (throw 'next nil))
214 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
215 ;; At correct date already, do nothing.
216 (goto-char pos)
217 (throw 'next nil))
218 ;; OK, we need to refile this entry.
219 (goto-char hdl-pos)
220 (org-cut-subtree)
221 (save-excursion
222 (save-restriction
223 (org-datetree-file-entry-under (current-kill 0) date)))))))))
225 (provide 'org-datetree)
227 ;; Local variables:
228 ;; generated-autoload-file: "org-loaddefs.el"
229 ;; End:
231 ;;; org-datetree.el ends here