org-datetree.el 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ;;; org-datetree.el --- Create date entries in a tree -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://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 (regex year &optional month day insert)
  130. "Find the datetree matched by REGEX for YEAR, MONTH, or DAY.
  131. REGEX is passed to `format' with YEAR, MONTH, and DAY as
  132. arguments. Match group 1 is compared against the specified date
  133. component. If INSERT is non-nil and there is no match then it is
  134. inserted into the buffer."
  135. (when (or month day)
  136. (org-narrow-to-subtree))
  137. (let ((re (format regex year month day))
  138. match)
  139. (goto-char (point-min))
  140. (while (and (setq match (re-search-forward re nil t))
  141. (goto-char (match-beginning 1))
  142. (< (string-to-number (match-string 1)) (or day month year))))
  143. (cond
  144. ((not match)
  145. (goto-char (point-max))
  146. (unless (bolp) (insert "\n"))
  147. (org-datetree-insert-line year month day insert))
  148. ((= (string-to-number (match-string 1)) (or day month year))
  149. (beginning-of-line))
  150. (t
  151. (beginning-of-line)
  152. (org-datetree-insert-line year month day insert)))))
  153. (defun org-datetree-insert-line (year &optional month day text)
  154. (delete-region (save-excursion (skip-chars-backward " \t\n") (point)) (point))
  155. (insert "\n" (make-string org-datetree-base-level ?*) " \n")
  156. (backward-char)
  157. (when month (org-do-demote))
  158. (when day (org-do-demote))
  159. (if text
  160. (insert text)
  161. (insert (format "%d" year))
  162. (when month
  163. (insert
  164. (if day
  165. (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year))
  166. (format-time-string "-%m %B" (encode-time 0 0 0 1 month year))))))
  167. (when (and day org-datetree-add-timestamp)
  168. (save-excursion
  169. (insert "\n")
  170. (org-indent-line)
  171. (org-insert-time-stamp
  172. (encode-time 0 0 0 day month year)
  173. nil
  174. (eq org-datetree-add-timestamp 'inactive))))
  175. (beginning-of-line))
  176. (defun org-datetree-file-entry-under (txt d)
  177. "Insert a node TXT into the date tree under date D."
  178. (org-datetree-find-date-create d)
  179. (let ((level (org-get-valid-level (funcall outline-level) 1)))
  180. (org-end-of-subtree t t)
  181. (org-back-over-empty-lines)
  182. (org-paste-subtree level txt)))
  183. (defun org-datetree-cleanup ()
  184. "Make sure all entries in the current tree are under the correct date.
  185. It may be useful to restrict the buffer to the applicable portion
  186. before running this command, even though the command tries to be smart."
  187. (interactive)
  188. (goto-char (point-min))
  189. (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
  190. (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'")))
  191. (while (re-search-forward org-ts-regexp nil t)
  192. (catch 'next
  193. (let ((tmp (buffer-substring
  194. (max (line-beginning-position)
  195. (- (match-beginning 0) org-ds-keyword-length))
  196. (match-beginning 0))))
  197. (when (or (string-suffix-p "-" tmp)
  198. (string-match dre tmp)
  199. (string-match sre tmp))
  200. (throw 'next nil))
  201. (let* ((dct (decode-time (org-time-string-to-time (match-string 0))))
  202. (date (list (nth 4 dct) (nth 3 dct) (nth 5 dct)))
  203. (year (nth 2 date))
  204. (month (car date))
  205. (day (nth 1 date))
  206. (pos (point))
  207. (hdl-pos (progn (org-back-to-heading t) (point))))
  208. (unless (org-up-heading-safe)
  209. ;; No parent, we are not in a date tree.
  210. (goto-char pos)
  211. (throw 'next nil))
  212. (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
  213. ;; Parent looks wrong, we are not in a date tree.
  214. (goto-char pos)
  215. (throw 'next nil))
  216. (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
  217. ;; At correct date already, do nothing.
  218. (goto-char pos)
  219. (throw 'next nil))
  220. ;; OK, we need to refile this entry.
  221. (goto-char hdl-pos)
  222. (org-cut-subtree)
  223. (save-excursion
  224. (save-restriction
  225. (org-datetree-file-entry-under (current-kill 0) date)))))))))
  226. (provide 'org-datetree)
  227. ;; Local variables:
  228. ;; generated-autoload-file: "org-loaddefs.el"
  229. ;; End:
  230. ;;; org-datetree.el ends here