org-datetree.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ;;; org-datetree.el --- Create date entries in a tree
  2. ;; Copyright (C) 2009-2013 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 DATE_TREE
  30. property (any value), the date tree will become a subtree under that entry,
  31. so the base level will be properly adjusted.")
  32. (defcustom org-datetree-add-timestamp nil
  33. "When non-nil, add a time stamp when create a datetree entry."
  34. :group 'org-capture
  35. :version "24.3"
  36. :type '(choice
  37. (const :tag "Do not add a time stamp" nil)
  38. (const :tag "Add an inactive time stamp" inactive)
  39. (const :tag "Add an active time stamp" active)))
  40. ;;;###autoload
  41. (defun org-datetree-find-date-create (date &optional keep-restriction)
  42. "Find or create an entry for DATE.
  43. If KEEP-RESTRICTION is non-nil, do not widen the buffer.
  44. When it is nil, the buffer will be widened to make sure an existing date
  45. tree can be found."
  46. (let ((year (nth 2 date))
  47. (month (car date))
  48. (day (nth 1 date)))
  49. (org-set-local 'org-datetree-base-level 1)
  50. (or keep-restriction (widen))
  51. (goto-char (point-min))
  52. (save-restriction
  53. (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t)
  54. (org-back-to-heading t)
  55. (org-set-local 'org-datetree-base-level
  56. (org-get-valid-level (funcall outline-level) 1))
  57. (org-narrow-to-subtree))
  58. (goto-char (point-min))
  59. (org-datetree-find-year-create year)
  60. (org-datetree-find-month-create year month)
  61. (org-datetree-find-day-create year month day)
  62. (goto-char (prog1 (point) (widen))))))
  63. (defun org-datetree-find-year-create (year)
  64. "Find the YEAR datetree or create it."
  65. (let ((re "^\\*+[ \t]+\\([12][0-9]\\{3\\}\\)\\(\\s-*?\\([ \t]:[[:alnum:]:_@#%]+:\\)?\\s-*$\\)")
  66. match)
  67. (goto-char (point-min))
  68. (while (and (setq match (re-search-forward re nil t))
  69. (goto-char (match-beginning 1))
  70. (< (string-to-number (match-string 1)) year)))
  71. (cond
  72. ((not match)
  73. (goto-char (point-max))
  74. (or (bolp) (newline))
  75. (org-datetree-insert-line year))
  76. ((= (string-to-number (match-string 1)) year)
  77. (goto-char (point-at-bol)))
  78. (t
  79. (beginning-of-line 1)
  80. (org-datetree-insert-line year)))))
  81. (defun org-datetree-find-month-create (year month)
  82. "Find the datetree for YEAR and MONTH or create it."
  83. (org-narrow-to-subtree)
  84. (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year))
  85. match)
  86. (goto-char (point-min))
  87. (while (and (setq match (re-search-forward re nil t))
  88. (goto-char (match-beginning 1))
  89. (< (string-to-number (match-string 1)) month)))
  90. (cond
  91. ((not match)
  92. (goto-char (point-max))
  93. (or (bolp) (newline))
  94. (org-datetree-insert-line year month))
  95. ((= (string-to-number (match-string 1)) month)
  96. (goto-char (point-at-bol)))
  97. (t
  98. (beginning-of-line 1)
  99. (org-datetree-insert-line year month)))))
  100. (defun org-datetree-find-day-create (year month day)
  101. "Find the datetree for YEAR, MONTH and DAY or create it."
  102. (org-narrow-to-subtree)
  103. (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month))
  104. match)
  105. (goto-char (point-min))
  106. (while (and (setq match (re-search-forward re nil t))
  107. (goto-char (match-beginning 1))
  108. (< (string-to-number (match-string 1)) day)))
  109. (cond
  110. ((not match)
  111. (goto-char (point-max))
  112. (or (bolp) (newline))
  113. (org-datetree-insert-line year month day))
  114. ((= (string-to-number (match-string 1)) day)
  115. (goto-char (point-at-bol)))
  116. (t
  117. (beginning-of-line 1)
  118. (org-datetree-insert-line year month day)))))
  119. (defun org-datetree-insert-line (year &optional month day)
  120. (let ((pos (point)) ts-type)
  121. (skip-chars-backward " \t\n")
  122. (delete-region (point) pos)
  123. (insert "\n" (make-string org-datetree-base-level ?*) " \n")
  124. (backward-char 1)
  125. (if month (org-do-demote))
  126. (if day (org-do-demote))
  127. (insert (format "%d" year))
  128. (when month
  129. (insert (format "-%02d" month))
  130. (if day
  131. (insert (format "-%02d %s"
  132. day (format-time-string
  133. "%A" (encode-time 0 0 0 day month year))))
  134. (insert (format " %s"
  135. (format-time-string
  136. "%B" (encode-time 0 0 0 1 month year))))))
  137. (when (and day (setq ts-type org-datetree-add-timestamp))
  138. (insert "\n")
  139. (org-indent-line)
  140. (org-insert-time-stamp (encode-time 0 0 0 day month year) nil ts-type))
  141. (beginning-of-line 1)))
  142. (defun org-datetree-file-entry-under (txt date)
  143. "Insert a node TXT into the date tree under DATE."
  144. (org-datetree-find-date-create date)
  145. (let ((level (org-get-valid-level (funcall outline-level) 1)))
  146. (org-end-of-subtree t t)
  147. (org-back-over-empty-lines)
  148. (org-paste-subtree level txt)))
  149. (defun org-datetree-cleanup ()
  150. "Make sure all entries in the current tree are under the correct date.
  151. It may be useful to restrict the buffer to the applicable portion
  152. before running this command, even though the command tries to be smart."
  153. (interactive)
  154. (goto-char (point-min))
  155. (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
  156. (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))
  157. dct ts tmp date year month day pos hdl-pos)
  158. (while (re-search-forward org-ts-regexp nil t)
  159. (catch 'next
  160. (setq ts (match-string 0))
  161. (setq tmp (buffer-substring
  162. (max (point-at-bol) (- (match-beginning 0)
  163. org-ds-keyword-length))
  164. (match-beginning 0)))
  165. (if (or (string-match "-\\'" tmp)
  166. (string-match dre tmp)
  167. (string-match sre tmp))
  168. (throw 'next nil))
  169. (setq dct (decode-time (org-time-string-to-time (match-string 0)))
  170. date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))
  171. year (nth 2 date)
  172. month (car date)
  173. day (nth 1 date)
  174. pos (point))
  175. (org-back-to-heading t)
  176. (setq hdl-pos (point))
  177. (unless (org-up-heading-safe)
  178. ;; No parent, we are not in a date tree
  179. (goto-char pos)
  180. (throw 'next nil))
  181. (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
  182. ;; Parent looks wrong, we are not in a date tree
  183. (goto-char pos)
  184. (throw 'next nil))
  185. (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
  186. ;; At correct date already, do nothing
  187. (progn (goto-char pos) (throw 'next nil)))
  188. ;; OK, we need to refile this entry
  189. (goto-char hdl-pos)
  190. (org-cut-subtree)
  191. (save-excursion
  192. (save-restriction
  193. (org-datetree-file-entry-under (current-kill 0) date)))))))
  194. (provide 'org-datetree)
  195. ;; Local variables:
  196. ;; generated-autoload-file: "org-loaddefs.el"
  197. ;; End:
  198. ;;; org-datetree.el ends here