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