org-datetree.el 10 KB

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