lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / org-datetree.el
blobb4797de1e58472abbc7e8b02f24534aa4c8485ef
1 ;;; org-datetree.el --- Create date entries in a tree -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://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 <https://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. If it is the symbol `subtree-at-point', then the tree
58 will be built under the headline at point."
59 (setq-local org-datetree-base-level 1)
60 (save-restriction
61 (if (eq keep-restriction 'subtree-at-point)
62 (progn
63 (unless (org-at-heading-p) (error "Not at heading"))
64 (widen)
65 (org-narrow-to-subtree)
66 (setq-local org-datetree-base-level
67 (org-get-valid-level (org-current-level) 1)))
68 (unless keep-restriction (widen))
69 ;; Support the old way of tree placement, using a property
70 (let ((prop (org-find-property "DATE_TREE")))
71 (when prop
72 (goto-char prop)
73 (setq-local org-datetree-base-level
74 (org-get-valid-level (org-current-level) 1))
75 (org-narrow-to-subtree))))
76 (goto-char (point-min))
77 (let ((year (calendar-extract-year d))
78 (month (calendar-extract-month d))
79 (day (calendar-extract-day d)))
80 (org-datetree--find-create
81 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
82 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
83 year)
84 (org-datetree--find-create
85 "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$"
86 year month)
87 (org-datetree--find-create
88 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
89 year month day))))
91 ;;;###autoload
92 (defun org-datetree-find-iso-week-create (d &optional keep-restriction)
93 "Find or create an ISO week entry for date D.
94 Compared to `org-datetree-find-date-create' this function creates
95 entries ordered by week instead of months.
96 When it is nil, the buffer will be widened to make sure an existing date
97 tree can be found. If it is the symbol `subtree-at-point', then the tree
98 will be built under the headline at point."
99 (setq-local org-datetree-base-level 1)
100 (save-restriction
101 (if (eq keep-restriction 'subtree-at-point)
102 (progn
103 (unless (org-at-heading-p) (error "Not at heading"))
104 (widen)
105 (org-narrow-to-subtree)
106 (setq-local org-datetree-base-level
107 (org-get-valid-level (org-current-level) 1)))
108 (unless keep-restriction (widen))
109 ;; Support the old way of tree placement, using a property
110 (let ((prop (org-find-property "WEEK_TREE")))
111 (when prop
112 (goto-char prop)
113 (setq-local org-datetree-base-level
114 (org-get-valid-level (org-current-level) 1))
115 (org-narrow-to-subtree))))
116 (goto-char (point-min))
117 (require 'cal-iso)
118 (let* ((year (calendar-extract-year d))
119 (month (calendar-extract-month d))
120 (day (calendar-extract-day d))
121 (time (encode-time 0 0 0 day month year))
122 (iso-date (calendar-iso-from-absolute
123 (calendar-absolute-from-gregorian d)))
124 (weekyear (nth 2 iso-date))
125 (week (nth 0 iso-date)))
126 ;; ISO 8601 week format is %G-W%V(-%u)
127 (org-datetree--find-create
128 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\
129 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)"
130 weekyear nil nil
131 (format-time-string "%G" time))
132 (org-datetree--find-create
133 "^\\*+[ \t]+%d-W\\([0-5][0-9]\\)$"
134 weekyear week nil
135 (format-time-string "%G-W%V" time))
136 ;; For the actual day we use the regular date instead of ISO week.
137 (org-datetree--find-create
138 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$"
139 year month day))))
141 (defun org-datetree--find-create
142 (regex-template year &optional month day insert)
143 "Find the datetree matched by REGEX-TEMPLATE for YEAR, MONTH, or DAY.
144 REGEX-TEMPLATE is passed to `format' with YEAR, MONTH, and DAY as
145 arguments. Match group 1 is compared against the specified date
146 component. If INSERT is non-nil and there is no match then it is
147 inserted into the buffer."
148 (when (or month day)
149 (org-narrow-to-subtree))
150 (let ((re (format regex-template year month day))
151 match)
152 (goto-char (point-min))
153 (while (and (setq match (re-search-forward re nil t))
154 (goto-char (match-beginning 1))
155 (< (string-to-number (match-string 1)) (or day month year))))
156 (cond
157 ((not match)
158 (goto-char (point-max))
159 (unless (bolp) (insert "\n"))
160 (org-datetree-insert-line year month day insert))
161 ((= (string-to-number (match-string 1)) (or day month year))
162 (beginning-of-line))
164 (beginning-of-line)
165 (org-datetree-insert-line year month day insert)))))
167 (defun org-datetree-insert-line (year &optional month day text)
168 (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point))
169 (insert "\n" (make-string org-datetree-base-level ?*) " \n")
170 (backward-char)
171 (when month (org-do-demote))
172 (when day (org-do-demote))
173 (if text
174 (insert text)
175 (insert (format "%d" year))
176 (when month
177 (insert
178 (if day
179 (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year))
180 (format-time-string "-%m %B" (encode-time 0 0 0 1 month year))))))
181 (when (and day org-datetree-add-timestamp)
182 (save-excursion
183 (insert "\n")
184 (org-indent-line)
185 (org-insert-time-stamp
186 (encode-time 0 0 0 day month year)
188 (eq org-datetree-add-timestamp 'inactive))))
189 (beginning-of-line))
191 (defun org-datetree-file-entry-under (txt d)
192 "Insert a node TXT into the date tree under date D."
193 (org-datetree-find-date-create d)
194 (let ((level (org-get-valid-level (funcall outline-level) 1)))
195 (org-end-of-subtree t t)
196 (org-back-over-empty-lines)
197 (org-paste-subtree level txt)))
199 (defun org-datetree-cleanup ()
200 "Make sure all entries in the current tree are under the correct date.
201 It may be useful to restrict the buffer to the applicable portion
202 before running this command, even though the command tries to be smart."
203 (interactive)
204 (goto-char (point-min))
205 (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
206 (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'")))
207 (while (re-search-forward org-ts-regexp nil t)
208 (catch 'next
209 (let ((tmp (buffer-substring
210 (max (line-beginning-position)
211 (- (match-beginning 0) org-ds-keyword-length))
212 (match-beginning 0))))
213 (when (or (string-suffix-p "-" tmp)
214 (string-match dre tmp)
215 (string-match sre tmp))
216 (throw 'next nil))
217 (let* ((dct (decode-time (org-time-string-to-time (match-string 0))))
218 (date (list (nth 4 dct) (nth 3 dct) (nth 5 dct)))
219 (year (nth 2 date))
220 (month (car date))
221 (day (nth 1 date))
222 (pos (point))
223 (hdl-pos (progn (org-back-to-heading t) (point))))
224 (unless (org-up-heading-safe)
225 ;; No parent, we are not in a date tree.
226 (goto-char pos)
227 (throw 'next nil))
228 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
229 ;; Parent looks wrong, we are not in a date tree.
230 (goto-char pos)
231 (throw 'next nil))
232 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
233 ;; At correct date already, do nothing.
234 (goto-char pos)
235 (throw 'next nil))
236 ;; OK, we need to refile this entry.
237 (goto-char hdl-pos)
238 (org-cut-subtree)
239 (save-excursion
240 (save-restriction
241 (org-datetree-file-entry-under (current-kill 0) date)))))))))
243 (provide 'org-datetree)
245 ;; Local variables:
246 ;; generated-autoload-file: "org-loaddefs.el"
247 ;; End:
249 ;;; org-datetree.el ends here