org-datetree.el (10304B)
1 ;;; org-datetree.el --- Create date entries in a tree -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2009-2023 Free Software Foundation, Inc. 4 5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com> 6 ;; Keywords: outlines, hypermedia, calendar, wp 7 ;; URL: https://orgmode.org 8 ;; 9 ;; This file is part of GNU Emacs. 10 ;; 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. 15 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. 20 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 24 ;; 25 ;;; Commentary: 26 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. 30 31 ;;; Code: 32 33 (require 'org-macs) 34 (org-assert-version) 35 36 (require 'org) 37 38 (defvar org-datetree-base-level 1 39 "The level at which years should be placed in the date tree. 40 This is normally one, but if the buffer has an entry with a 41 DATE_TREE (or WEEK_TREE for ISO week entries) property (any 42 value), the date tree will become a subtree under that entry, so 43 the base level will be properly adjusted.") 44 45 (defcustom org-datetree-add-timestamp nil 46 "When non-nil, add a time stamp matching date of entry. 47 Added time stamp is active unless value is `inactive'." 48 :group 'org-capture 49 :version "24.3" 50 :type '(choice 51 (const :tag "Do not add a time stamp" nil) 52 (const :tag "Add an inactive time stamp" inactive) 53 (const :tag "Add an active time stamp" active))) 54 55 ;;;###autoload 56 (defun org-datetree-find-date-create (d &optional keep-restriction) 57 "Find or create a day entry for date D. 58 If KEEP-RESTRICTION is non-nil, do not widen the buffer. 59 When it is nil, the buffer will be widened to make sure an existing date 60 tree can be found. If it is the symbol `subtree-at-point', then the tree 61 will be built under the headline at point." 62 (org-datetree--find-create-group d 'day keep-restriction)) 63 64 ;;;###autoload 65 (defun org-datetree-find-month-create (d &optional keep-restriction) 66 "Find or create a month entry for date D. 67 Compared to `org-datetree-find-date-create' this function creates 68 entries grouped by month instead of days. 69 If KEEP-RESTRICTION is non-nil, do not widen the buffer. 70 When it is nil, the buffer will be widened to make sure an existing date 71 tree can be found. If it is the symbol `subtree-at-point', then the tree 72 will be built under the headline at point." 73 (org-datetree--find-create-group d 'month keep-restriction)) 74 75 (defun org-datetree--find-create-group 76 (d time-grouping &optional keep-restriction) 77 "Find or create an entry for date D. 78 If time-period is day, group entries by day. 79 If time-period is month, then group entries by month." 80 (setq-local org-datetree-base-level 1) 81 (save-restriction 82 (if (eq keep-restriction 'subtree-at-point) 83 (progn 84 (unless (org-at-heading-p) (error "Not at heading")) 85 (widen) 86 (org-narrow-to-subtree) 87 (setq-local org-datetree-base-level 88 (org-get-valid-level (org-current-level) 1))) 89 (unless keep-restriction (widen)) 90 ;; Support the old way of tree placement, using a property 91 (let ((prop (org-find-property "DATE_TREE"))) 92 (when prop 93 (goto-char prop) 94 (setq-local org-datetree-base-level 95 (org-get-valid-level (org-current-level) 1)) 96 (org-narrow-to-subtree)))) 97 (goto-char (point-min)) 98 (let ((year (calendar-extract-year d)) 99 (month (calendar-extract-month d)) 100 (day (calendar-extract-day d))) 101 (org-datetree--find-create 102 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\ 103 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)" 104 year) 105 (org-datetree--find-create 106 "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" 107 year month) 108 (when (eq time-grouping 'day) 109 (org-datetree--find-create 110 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" 111 year month day))))) 112 113 ;;;###autoload 114 (defun org-datetree-find-iso-week-create (d &optional keep-restriction) 115 "Find or create an ISO week entry for date D. 116 Compared to `org-datetree-find-date-create' this function creates 117 entries ordered by week instead of months. 118 When it is nil, the buffer will be widened to make sure an existing date 119 tree can be found. If it is the symbol `subtree-at-point', then the tree 120 will be built under the headline at point." 121 (setq-local org-datetree-base-level 1) 122 (save-restriction 123 (if (eq keep-restriction 'subtree-at-point) 124 (progn 125 (unless (org-at-heading-p) (error "Not at heading")) 126 (widen) 127 (org-narrow-to-subtree) 128 (setq-local org-datetree-base-level 129 (org-get-valid-level (org-current-level) 1))) 130 (unless keep-restriction (widen)) 131 ;; Support the old way of tree placement, using a property 132 (let ((prop (org-find-property "WEEK_TREE"))) 133 (when prop 134 (goto-char prop) 135 (setq-local org-datetree-base-level 136 (org-get-valid-level (org-current-level) 1)) 137 (org-narrow-to-subtree)))) 138 (goto-char (point-min)) 139 (require 'cal-iso) 140 (let* ((year (calendar-extract-year d)) 141 (month (calendar-extract-month d)) 142 (day (calendar-extract-day d)) 143 (time (org-encode-time 0 0 0 day month year)) 144 (iso-date (calendar-iso-from-absolute 145 (calendar-absolute-from-gregorian d))) 146 (weekyear (nth 2 iso-date)) 147 (week (nth 0 iso-date))) 148 ;; ISO 8601 week format is %G-W%V(-%u) 149 (org-datetree--find-create 150 "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\ 151 \\([ \t]:[[:alnum:]:_@#%%]+:\\)?\\s-*$\\)" 152 weekyear nil nil 153 (format-time-string "%G" time)) 154 (org-datetree--find-create 155 "^\\*+[ \t]+%d-W\\([0-5][0-9]\\)$" 156 weekyear week nil 157 (format-time-string "%G-W%V" time)) 158 ;; For the actual day we use the regular date instead of ISO week. 159 (org-datetree--find-create 160 "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" 161 year month day)))) 162 163 (defun org-datetree--find-create 164 (regex-template year &optional month day insert) 165 "Find the datetree matched by REGEX-TEMPLATE for YEAR, MONTH, or DAY. 166 REGEX-TEMPLATE is passed to `format' with YEAR, MONTH, and DAY as 167 arguments. Match group 1 is compared against the specified date 168 component. If INSERT is non-nil and there is no match then it is 169 inserted into the buffer." 170 (when (or month day) 171 (org-narrow-to-subtree)) 172 (let ((re (format regex-template year month day)) 173 match) 174 (goto-char (point-min)) 175 (while (and (setq match (re-search-forward re nil t)) 176 (goto-char (match-beginning 1)) 177 (< (string-to-number (match-string 1)) (or day month year)))) 178 (cond 179 ((not match) 180 (goto-char (point-max)) 181 (unless (bolp) (insert "\n")) 182 (org-datetree-insert-line year month day insert)) 183 ((= (string-to-number (match-string 1)) (or day month year)) 184 (beginning-of-line)) 185 (t 186 (beginning-of-line) 187 (org-datetree-insert-line year month day insert))))) 188 189 (defun org-datetree-insert-line (year &optional month day text) 190 (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point)) 191 (when (org--blank-before-heading-p) (insert "\n")) 192 (insert "\n" (make-string org-datetree-base-level ?*) " \n") 193 (backward-char) 194 (when month (org-do-demote)) 195 (when day (org-do-demote)) 196 (if text 197 (insert text) 198 (insert (format "%d" year)) 199 (when month 200 (insert 201 (if day 202 (format-time-string "-%m-%d %A" (org-encode-time 0 0 0 day month year)) 203 (format-time-string "-%m %B" (org-encode-time 0 0 0 1 month year)))))) 204 (when (and day org-datetree-add-timestamp) 205 (save-excursion 206 (insert "\n") 207 (org-indent-line) 208 (org-insert-time-stamp 209 (org-encode-time 0 0 0 day month year) 210 nil 211 (eq org-datetree-add-timestamp 'inactive)))) 212 (beginning-of-line)) 213 214 (defun org-datetree-file-entry-under (txt d) 215 "Insert a node TXT into the date tree under date D." 216 (org-datetree-find-date-create d) 217 (let ((level (org-get-valid-level (funcall outline-level) 1))) 218 (org-end-of-subtree t t) 219 (org-back-over-empty-lines) 220 (org-paste-subtree level txt))) 221 222 (defun org-datetree-cleanup () 223 "Make sure all entries in the current tree are under the correct date. 224 It may be useful to restrict the buffer to the applicable portion 225 before running this command, even though the command tries to be smart." 226 (interactive) 227 (goto-char (point-min)) 228 (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'")) 229 (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))) 230 (while (re-search-forward org-ts-regexp nil t) 231 (catch 'next 232 (let ((tmp (buffer-substring 233 (max (line-beginning-position) 234 (- (match-beginning 0) org-ds-keyword-length)) 235 (match-beginning 0)))) 236 (when (or (string-suffix-p "-" tmp) 237 (string-match dre tmp) 238 (string-match sre tmp)) 239 (throw 'next nil)) 240 (let* ((dct (decode-time (org-time-string-to-time (match-string 0)))) 241 (date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))) 242 (year (nth 2 date)) 243 (month (car date)) 244 (day (nth 1 date)) 245 (pos (point)) 246 (hdl-pos (progn (org-back-to-heading t) (point)))) 247 (unless (org-up-heading-safe) 248 ;; No parent, we are not in a date tree. 249 (goto-char pos) 250 (throw 'next nil)) 251 (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]") 252 ;; Parent looks wrong, we are not in a date tree. 253 (goto-char pos) 254 (throw 'next nil)) 255 (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day)) 256 ;; At correct date already, do nothing. 257 (goto-char pos) 258 (throw 'next nil)) 259 ;; OK, we need to refile this entry. 260 (goto-char hdl-pos) 261 (org-cut-subtree) 262 (save-excursion 263 (save-restriction 264 (org-datetree-file-entry-under (current-kill 0) date))))))))) 265 266 (provide 'org-datetree) 267 268 ;; Local variables: 269 ;; generated-autoload-file: "org-loaddefs.el" 270 ;; End: 271 272 ;;; org-datetree.el ends here