org-datetree.el 10 KB

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