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