org-datetree.el 9.1 KB

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