org-datetree.el 6.8 KB

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