Merge branch 'maint'
[org-mode.git] / lisp / org-datetree.el
blobe15d7858fcb655276b74b395da0e334c643dfb12
1 ;;; org-datetree.el --- Create date entries in a tree
3 ;; Copyright (C) 2009-2015 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 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 matching date of entry.
43 Added time stamp is active unless value is `inactive'."
44 :group 'org-capture
45 :version "24.3"
46 :type '(choice
47 (const :tag "Do not add a time stamp" nil)
48 (const :tag "Add an inactive time stamp" inactive)
49 (const :tag "Add an active time stamp" active)))
51 ;;;###autoload
52 (defun org-datetree-find-date-create (date &optional keep-restriction)
53 "Find or create an entry for DATE.
54 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
55 When it is nil, the buffer will be widened to make sure an existing date
56 tree can be found."
57 (let ((year (nth 2 date))
58 (month (car date))
59 (day (nth 1 date)))
60 (org-set-local 'org-datetree-base-level 1)
61 (or keep-restriction (widen))
62 (goto-char (point-min))
63 (save-restriction
64 (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t)
65 (org-back-to-heading t)
66 (org-set-local 'org-datetree-base-level
67 (org-get-valid-level (funcall outline-level) 1))
68 (org-narrow-to-subtree))
69 (goto-char (point-min))
70 (org-datetree-find-year-create year)
71 (org-datetree-find-month-create year month)
72 (org-datetree-find-day-create year month day)
73 (goto-char (prog1 (point) (widen))))))
75 (defun org-datetree-find-year-create (year)
76 "Find the YEAR datetree or create it."
77 (let ((re "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\\([ \t]:[[:alnum:]:_@#%]+:\\)?\\s-*$\\)")
78 match)
79 (goto-char (point-min))
80 (while (and (setq match (re-search-forward re nil t))
81 (goto-char (match-beginning 1))
82 (< (string-to-number (match-string 1)) year)))
83 (cond
84 ((not match)
85 (goto-char (point-max))
86 (or (bolp) (newline))
87 (org-datetree-insert-line year))
88 ((= (string-to-number (match-string 1)) year)
89 (goto-char (point-at-bol)))
91 (beginning-of-line 1)
92 (org-datetree-insert-line year)))))
94 (defun org-datetree-find-month-create (year month)
95 "Find the datetree for YEAR and MONTH or create it."
96 (org-narrow-to-subtree)
97 (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year))
98 match)
99 (goto-char (point-min))
100 (while (and (setq match (re-search-forward re nil t))
101 (goto-char (match-beginning 1))
102 (< (string-to-number (match-string 1)) month)))
103 (cond
104 ((not match)
105 (goto-char (point-max))
106 (or (bolp) (newline))
107 (org-datetree-insert-line year month))
108 ((= (string-to-number (match-string 1)) month)
109 (goto-char (point-at-bol)))
111 (beginning-of-line 1)
112 (org-datetree-insert-line year month)))))
114 (defun org-datetree-find-day-create (year month day)
115 "Find the datetree for YEAR, MONTH and DAY or create it."
116 (org-narrow-to-subtree)
117 (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month))
118 match)
119 (goto-char (point-min))
120 (while (and (setq match (re-search-forward re nil t))
121 (goto-char (match-beginning 1))
122 (< (string-to-number (match-string 1)) day)))
123 (cond
124 ((not match)
125 (goto-char (point-max))
126 (or (bolp) (newline))
127 (org-datetree-insert-line year month day))
128 ((= (string-to-number (match-string 1)) day)
129 (goto-char (point-at-bol)))
131 (beginning-of-line 1)
132 (org-datetree-insert-line year month day)))))
134 (defun org-datetree-insert-line (year &optional month day)
135 (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point))
136 (insert "\n" (make-string org-datetree-base-level ?*) " \n")
137 (backward-char)
138 (when month (org-do-demote))
139 (when day (org-do-demote))
140 (insert (format "%d" year))
141 (when month
142 (insert
143 (format "-%02d" month)
144 (if day
145 (format "-%02d %s"
147 (format-time-string "%A" (encode-time 0 0 0 day month year)))
148 (format " %s"
149 (format-time-string "%B" (encode-time 0 0 0 1 month year))))))
150 (when (and day org-datetree-add-timestamp)
151 (save-excursion
152 (insert "\n")
153 (org-indent-line)
154 (org-insert-time-stamp
155 (encode-time 0 0 0 day month year)
157 (eq org-datetree-add-timestamp 'inactive))))
158 (beginning-of-line))
160 (defun org-datetree-file-entry-under (txt date)
161 "Insert a node TXT into the date tree under DATE."
162 (org-datetree-find-date-create date)
163 (let ((level (org-get-valid-level (funcall outline-level) 1)))
164 (org-end-of-subtree t t)
165 (org-back-over-empty-lines)
166 (org-paste-subtree level txt)))
168 (defun org-datetree-cleanup ()
169 "Make sure all entries in the current tree are under the correct date.
170 It may be useful to restrict the buffer to the applicable portion
171 before running this command, even though the command tries to be smart."
172 (interactive)
173 (goto-char (point-min))
174 (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
175 (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))
176 dct ts tmp date year month day pos hdl-pos)
177 (while (re-search-forward org-ts-regexp nil t)
178 (catch 'next
179 (setq ts (match-string 0))
180 (setq tmp (buffer-substring
181 (max (point-at-bol) (- (match-beginning 0)
182 org-ds-keyword-length))
183 (match-beginning 0)))
184 (if (or (string-match "-\\'" tmp)
185 (string-match dre tmp)
186 (string-match sre tmp))
187 (throw 'next nil))
188 (setq dct (decode-time (org-time-string-to-time (match-string 0)))
189 date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))
190 year (nth 2 date)
191 month (car date)
192 day (nth 1 date)
193 pos (point))
194 (org-back-to-heading t)
195 (setq hdl-pos (point))
196 (unless (org-up-heading-safe)
197 ;; No parent, we are not in a date tree
198 (goto-char pos)
199 (throw 'next nil))
200 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
201 ;; Parent looks wrong, we are not in a date tree
202 (goto-char pos)
203 (throw 'next nil))
204 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
205 ;; At correct date already, do nothing
206 (progn (goto-char pos) (throw 'next nil)))
207 ;; OK, we need to refile this entry
208 (goto-char hdl-pos)
209 (org-cut-subtree)
210 (save-excursion
211 (save-restriction
212 (org-datetree-file-entry-under (current-kill 0) date)))))))
214 (provide 'org-datetree)
216 ;; Local variables:
217 ;; generated-autoload-file: "org-loaddefs.el"
218 ;; End:
220 ;;; org-datetree.el ends here