org-datetree.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ;;; org-datetree.el --- Create date entries in a tree
  2. ;; Copyright (C) 2009-2012 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. (let ((re "^\\*+[ \t]+\\([12][0-9][0-9][0-9]\\)\\s-*$")
  65. match)
  66. (goto-char (point-min))
  67. (while (and (setq match (re-search-forward re nil t))
  68. (goto-char (match-beginning 1))
  69. (< (string-to-number (match-string 1)) year)))
  70. (cond
  71. ((not match)
  72. (goto-char (point-max))
  73. (or (bolp) (newline))
  74. (org-datetree-insert-line year))
  75. ((= (string-to-number (match-string 1)) year)
  76. (goto-char (point-at-bol)))
  77. (t
  78. (beginning-of-line 1)
  79. (org-datetree-insert-line year)))))
  80. (defun org-datetree-find-month-create (year month)
  81. (org-narrow-to-subtree)
  82. (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year))
  83. match)
  84. (goto-char (point-min))
  85. (while (and (setq match (re-search-forward re nil t))
  86. (goto-char (match-beginning 1))
  87. (< (string-to-number (match-string 1)) month)))
  88. (cond
  89. ((not match)
  90. (goto-char (point-max))
  91. (or (bolp) (newline))
  92. (org-datetree-insert-line year month))
  93. ((= (string-to-number (match-string 1)) month)
  94. (goto-char (point-at-bol)))
  95. (t
  96. (beginning-of-line 1)
  97. (org-datetree-insert-line year month)))))
  98. (defun org-datetree-find-day-create (year month day)
  99. (org-narrow-to-subtree)
  100. (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month))
  101. match)
  102. (goto-char (point-min))
  103. (while (and (setq match (re-search-forward re nil t))
  104. (goto-char (match-beginning 1))
  105. (< (string-to-number (match-string 1)) day)))
  106. (cond
  107. ((not match)
  108. (goto-char (point-max))
  109. (or (bolp) (newline))
  110. (org-datetree-insert-line year month day))
  111. ((= (string-to-number (match-string 1)) day)
  112. (goto-char (point-at-bol)))
  113. (t
  114. (beginning-of-line 1)
  115. (org-datetree-insert-line year month day)))))
  116. (defun org-datetree-insert-line (year &optional month day)
  117. (let ((pos (point)) ts-type)
  118. (skip-chars-backward " \t\n")
  119. (delete-region (point) pos)
  120. (insert "\n" (make-string org-datetree-base-level ?*) " \n")
  121. (backward-char 1)
  122. (if month (org-do-demote))
  123. (if day (org-do-demote))
  124. (insert (format "%d" year))
  125. (when month
  126. (insert (format "-%02d" month))
  127. (if day
  128. (insert (format "-%02d %s"
  129. day (format-time-string
  130. "%A" (encode-time 0 0 0 day month year))))
  131. (insert (format " %s"
  132. (format-time-string
  133. "%B" (encode-time 0 0 0 1 month year))))))
  134. (when (and day (setq ts-type org-datetree-add-timestamp))
  135. (insert "\n")
  136. (org-indent-line)
  137. (org-insert-time-stamp (encode-time 0 0 0 day month year) nil ts-type))
  138. (beginning-of-line 1)))
  139. (defun org-datetree-file-entry-under (txt date)
  140. "Insert a node TXT into the date tree under DATE."
  141. (org-datetree-find-date-create date)
  142. (let ((level (org-get-valid-level (funcall outline-level) 1)))
  143. (org-end-of-subtree t t)
  144. (org-back-over-empty-lines)
  145. (org-paste-subtree level txt)))
  146. (defun org-datetree-cleanup ()
  147. "Make sure all entries in the current tree are under the correct date.
  148. It may be useful to restrict the buffer to the applicable portion
  149. before running this command, even though the command tries to be smart."
  150. (interactive)
  151. (goto-char (point-min))
  152. (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
  153. (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))
  154. dct ts tmp date year month day pos hdl-pos)
  155. (while (re-search-forward org-ts-regexp nil t)
  156. (catch 'next
  157. (setq ts (match-string 0))
  158. (setq tmp (buffer-substring
  159. (max (point-at-bol) (- (match-beginning 0)
  160. org-ds-keyword-length))
  161. (match-beginning 0)))
  162. (if (or (string-match "-\\'" tmp)
  163. (string-match dre tmp)
  164. (string-match sre tmp))
  165. (throw 'next nil))
  166. (setq dct (decode-time (org-time-string-to-time (match-string 0)))
  167. date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))
  168. year (nth 2 date)
  169. month (car date)
  170. day (nth 1 date)
  171. pos (point))
  172. (org-back-to-heading t)
  173. (setq hdl-pos (point))
  174. (unless (org-up-heading-safe)
  175. ;; No parent, we are not in a date tree
  176. (goto-char pos)
  177. (throw 'next nil))
  178. (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
  179. ;; Parent looks wrong, we are not in a date tree
  180. (goto-char pos)
  181. (throw 'next nil))
  182. (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
  183. ;; At correct date already, do nothing
  184. (progn (goto-char pos) (throw 'next nil)))
  185. ;; OK, we need to refile this entry
  186. (goto-char hdl-pos)
  187. (org-cut-subtree)
  188. (save-excursion
  189. (save-restriction
  190. (org-datetree-file-entry-under (current-kill 0) date)))))))
  191. (provide 'org-datetree)
  192. ;;; org-datetree.el ends here