org-datetree.el 8.3 KB

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