* Makefile.in (GNULIB_MODULES): Change ftoastr to dtoastr.
[emacs.git] / lisp / org / org-datetree.el
blob8014f8f1f93aec33923f4de8ff2380b4f5a331a2
1 ;;; org-datetree.el --- Create date entries in a tree
3 ;; Copyright (C) 2009, 2010 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 ;; Version: 7.4
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains code to create entries in a tree where the top-level
29 ;; nodes represent years, the level 2 nodes represent the months, and the
30 ;; level 1 entries days.
32 ;;; Code:
34 (require 'org)
36 (defvar org-datetree-base-level 1
37 "The level at which years should be placed in the date tree.
38 This is normally one, but if the buffer has an entry with a DATE_TREE
39 property (any value), the date tree will become a subtree under that entry,
40 so the base level will be properly adjusted.")
42 ;;;###autoload
43 (defun org-datetree-find-date-create (date &optional keep-restriction)
44 "Find or create an entry for DATE.
45 If KEEP-RESTRICTION is non-nil, do not widen the buffer.
46 When it is nil, the buffer will be widened to make sure an existing date
47 tree can be found."
48 (let ((year (nth 2 date))
49 (month (car date))
50 (day (nth 1 date)))
51 (org-set-local 'org-datetree-base-level 1)
52 (or keep-restriction (widen))
53 (goto-char (point-min))
54 (save-restriction
55 (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t)
56 (org-back-to-heading t)
57 (org-set-local 'org-datetree-base-level
58 (org-get-valid-level (funcall outline-level) 1))
59 (org-narrow-to-subtree))
60 (goto-char (point-min))
61 (org-datetree-find-year-create year)
62 (org-datetree-find-month-create year month)
63 (org-datetree-find-day-create year month day)
64 (goto-char (prog1 (point) (widen))))))
66 (defun org-datetree-find-year-create (year)
67 (let ((re "^\\*+[ \t]+\\([12][0-9][0-9][0-9]\\)[ \t\n]")
68 match)
69 (goto-char (point-min))
70 (while (and (setq match (re-search-forward re nil t))
71 (goto-char (match-beginning 1))
72 (< (string-to-number (match-string 1)) year)))
73 (cond
74 ((not match)
75 (goto-char (point-max))
76 (or (bolp) (newline))
77 (org-datetree-insert-line year))
78 ((= (string-to-number (match-string 1)) year)
79 (goto-char (point-at-bol)))
81 (beginning-of-line 1)
82 (org-datetree-insert-line year)))))
84 (defun org-datetree-find-month-create (year month)
85 (org-narrow-to-subtree)
86 (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\)[ \t\n]" year))
87 match)
88 (goto-char (point-min))
89 (while (and (setq match (re-search-forward re nil t))
90 (goto-char (match-beginning 1))
91 (< (string-to-number (match-string 1)) month)))
92 (cond
93 ((not match)
94 (goto-char (point-max))
95 (or (bolp) (newline))
96 (org-datetree-insert-line year month))
97 ((= (string-to-number (match-string 1)) month)
98 (goto-char (point-at-bol)))
100 (beginning-of-line 1)
101 (org-datetree-insert-line year month)))))
103 (defun org-datetree-find-day-create (year month day)
104 (org-narrow-to-subtree)
105 (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\)[ \t\n]" year month))
106 match)
107 (goto-char (point-min))
108 (while (and (setq match (re-search-forward re nil t))
109 (goto-char (match-beginning 1))
110 (< (string-to-number (match-string 1)) day)))
111 (cond
112 ((not match)
113 (goto-char (point-max))
114 (or (bolp) (newline))
115 (org-datetree-insert-line year month day))
116 ((= (string-to-number (match-string 1)) day)
117 (goto-char (point-at-bol)))
119 (beginning-of-line 1)
120 (org-datetree-insert-line year month day)))))
122 (defun org-datetree-insert-line (year &optional month day)
123 (let ((pos (point)))
124 (skip-chars-backward " \t\n")
125 (delete-region (point) pos)
126 (insert "\n" (make-string org-datetree-base-level ?*) " \n")
127 (backward-char 1)
128 (if month (org-do-demote))
129 (if day (org-do-demote))
130 (insert (format "%d" year))
131 (when month
132 (insert (format "-%02d" month))
133 (if day
134 (insert (format "-%02d %s"
135 day (format-time-string
136 "%A" (encode-time 0 0 0 day month year))))
137 (insert (format " %s"
138 (format-time-string
139 "%B" (encode-time 0 0 0 1 month year))))))
140 (beginning-of-line 1)))
142 (defun org-datetree-file-entry-under (txt date)
143 "Insert a node TXT into the date tree under DATE."
144 (org-datetree-find-date-create date)
145 (let ((level (org-get-valid-level (funcall outline-level) 1)))
146 (org-end-of-subtree t t)
147 (org-back-over-empty-lines)
148 (org-paste-subtree level txt)))
150 (defun org-datetree-cleanup ()
151 "Make sure all entries in the current tree are under the correct date.
152 It may be useful to restrict the buffer to the applicable portion
153 before running this command, even though the command tries to be smart."
154 (interactive)
155 (goto-char (point-min))
156 (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
157 (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))
158 dct ts tmp date year month day pos hdl-pos)
159 (while (re-search-forward org-ts-regexp nil t)
160 (catch 'next
161 (setq ts (match-string 0))
162 (setq tmp (buffer-substring
163 (max (point-at-bol) (- (match-beginning 0)
164 org-ds-keyword-length))
165 (match-beginning 0)))
166 (if (or (string-match "-\\'" tmp)
167 (string-match dre tmp)
168 (string-match sre tmp))
169 (throw 'next nil))
170 (setq dct (decode-time (org-time-string-to-time (match-string 0)))
171 date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))
172 year (nth 2 date)
173 month (car date)
174 day (nth 1 date)
175 pos (point))
176 (org-back-to-heading t)
177 (setq hdl-pos (point))
178 (unless (org-up-heading-safe)
179 ;; No parent, we are not in a date tree
180 (goto-char pos)
181 (throw 'next nil))
182 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
183 ;; Parent looks wrong, we are not in a date tree
184 (goto-char pos)
185 (throw 'next nil))
186 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
187 ;; At correct date already, do nothing
188 (progn (goto-char pos) (throw 'next nil)))
189 ;; OK, we need to refile this entry
190 (goto-char hdl-pos)
191 (org-cut-subtree)
192 (save-excursion
193 (save-restriction
194 (org-datetree-file-entry-under (current-kill 0) date)))))))
196 (provide 'org-datetree)
198 ;; arch-tag: 1daea962-fd08-448b-9f98-6e8b511b3601
200 ;;; org-datetree.el ends here