org-datetree.el 8.3 KB

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