org-datetree.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; org-datetree.el --- Create date entries in a tree
  2. ;; Copyright (C) 2009, 2010 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. ;; Version: 7.5
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains code to create entries in a tree where the top-level
  24. ;; nodes represent years, the level 2 nodes represent the months, and the
  25. ;; level 1 entries days.
  26. ;;; Code:
  27. (require 'org)
  28. (defvar org-datetree-base-level 1
  29. "The level at which years should be placed in the date tree.
  30. This is normally one, but if the buffer has an entry with a DATE_TREE
  31. property (any value), the date tree will become a subtree under that entry,
  32. so the base level will be properly adjusted.")
  33. ;;;###autoload
  34. (defun org-datetree-find-date-create (date &optional keep-restriction)
  35. "Find or create an entry for DATE.
  36. If KEEP-RESTRICTION is non-nil, do not widen the buffer.
  37. When it is nil, the buffer will be widened to make sure an existing date
  38. tree can be found."
  39. (let ((year (nth 2 date))
  40. (month (car date))
  41. (day (nth 1 date)))
  42. (org-set-local 'org-datetree-base-level 1)
  43. (or keep-restriction (widen))
  44. (goto-char (point-min))
  45. (save-restriction
  46. (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t)
  47. (org-back-to-heading t)
  48. (org-set-local 'org-datetree-base-level
  49. (org-get-valid-level (funcall outline-level) 1))
  50. (org-narrow-to-subtree))
  51. (goto-char (point-min))
  52. (org-datetree-find-year-create year)
  53. (org-datetree-find-month-create year month)
  54. (org-datetree-find-day-create year month day)
  55. (goto-char (prog1 (point) (widen))))))
  56. (defun org-datetree-find-year-create (year)
  57. (let ((re "^\\*+[ \t]+\\([12][0-9][0-9][0-9]\\)$")
  58. match)
  59. (goto-char (point-min))
  60. (while (and (setq match (re-search-forward re nil t))
  61. (goto-char (match-beginning 1))
  62. (< (string-to-number (match-string 1)) year)))
  63. (cond
  64. ((not match)
  65. (goto-char (point-max))
  66. (or (bolp) (newline))
  67. (org-datetree-insert-line year))
  68. ((= (string-to-number (match-string 1)) year)
  69. (goto-char (point-at-bol)))
  70. (t
  71. (beginning-of-line 1)
  72. (org-datetree-insert-line year)))))
  73. (defun org-datetree-find-month-create (year month)
  74. (org-narrow-to-subtree)
  75. (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\) \\w+$" year))
  76. match)
  77. (goto-char (point-min))
  78. (while (and (setq match (re-search-forward re nil t))
  79. (goto-char (match-beginning 1))
  80. (< (string-to-number (match-string 1)) month)))
  81. (cond
  82. ((not match)
  83. (goto-char (point-max))
  84. (or (bolp) (newline))
  85. (org-datetree-insert-line year month))
  86. ((= (string-to-number (match-string 1)) month)
  87. (goto-char (point-at-bol)))
  88. (t
  89. (beginning-of-line 1)
  90. (org-datetree-insert-line year month)))))
  91. (defun org-datetree-find-day-create (year month day)
  92. (org-narrow-to-subtree)
  93. (let ((re (format "^\\*+[ \t]+%d-%02d-\\([0123][0-9]\\) \\w+$" year month))
  94. match)
  95. (goto-char (point-min))
  96. (while (and (setq match (re-search-forward re nil t))
  97. (goto-char (match-beginning 1))
  98. (< (string-to-number (match-string 1)) day)))
  99. (cond
  100. ((not match)
  101. (goto-char (point-max))
  102. (or (bolp) (newline))
  103. (org-datetree-insert-line year month day))
  104. ((= (string-to-number (match-string 1)) day)
  105. (goto-char (point-at-bol)))
  106. (t
  107. (beginning-of-line 1)
  108. (org-datetree-insert-line year month day)))))
  109. (defun org-datetree-insert-line (year &optional month day)
  110. (let ((pos (point)))
  111. (skip-chars-backward " \t\n")
  112. (delete-region (point) pos)
  113. (insert "\n" (make-string org-datetree-base-level ?*) " \n")
  114. (backward-char 1)
  115. (if month (org-do-demote))
  116. (if day (org-do-demote))
  117. (insert (format "%d" year))
  118. (when month
  119. (insert (format "-%02d" month))
  120. (if day
  121. (insert (format "-%02d %s"
  122. day (format-time-string
  123. "%A" (encode-time 0 0 0 day month year))))
  124. (insert (format " %s"
  125. (format-time-string
  126. "%B" (encode-time 0 0 0 1 month year))))))
  127. (beginning-of-line 1)))
  128. (defun org-datetree-file-entry-under (txt date)
  129. "Insert a node TXT into the date tree under DATE."
  130. (org-datetree-find-date-create date)
  131. (let ((level (org-get-valid-level (funcall outline-level) 1)))
  132. (org-end-of-subtree t t)
  133. (org-back-over-empty-lines)
  134. (org-paste-subtree level txt)))
  135. (defun org-datetree-cleanup ()
  136. "Make sure all entries in the current tree are under the correct date.
  137. It may be useful to restrict the buffer to the applicable portion
  138. before running this command, even though the command tries to be smart."
  139. (interactive)
  140. (goto-char (point-min))
  141. (let ((dre (concat "\\<" org-deadline-string "\\>[ \t]*\\'"))
  142. (sre (concat "\\<" org-scheduled-string "\\>[ \t]*\\'"))
  143. dct ts tmp date year month day pos hdl-pos)
  144. (while (re-search-forward org-ts-regexp nil t)
  145. (catch 'next
  146. (setq ts (match-string 0))
  147. (setq tmp (buffer-substring
  148. (max (point-at-bol) (- (match-beginning 0)
  149. org-ds-keyword-length))
  150. (match-beginning 0)))
  151. (if (or (string-match "-\\'" tmp)
  152. (string-match dre tmp)
  153. (string-match sre tmp))
  154. (throw 'next nil))
  155. (setq dct (decode-time (org-time-string-to-time (match-string 0)))
  156. date (list (nth 4 dct) (nth 3 dct) (nth 5 dct))
  157. year (nth 2 date)
  158. month (car date)
  159. day (nth 1 date)
  160. pos (point))
  161. (org-back-to-heading t)
  162. (setq hdl-pos (point))
  163. (unless (org-up-heading-safe)
  164. ;; No parent, we are not in a date tree
  165. (goto-char pos)
  166. (throw 'next nil))
  167. (unless (looking-at "\\*+[ \t]+[0-9]+-[0-1][0-9]-[0-3][0-9]")
  168. ;; Parent looks wrong, we are not in a date tree
  169. (goto-char pos)
  170. (throw 'next nil))
  171. (when (looking-at (format "\\*+[ \t]+%d-%02d-%02d" year month day))
  172. ;; At correct date already, do nothing
  173. (progn (goto-char pos) (throw 'next nil)))
  174. ;; OK, we need to refile this entry
  175. (goto-char hdl-pos)
  176. (org-cut-subtree)
  177. (save-excursion
  178. (save-restriction
  179. (org-datetree-file-entry-under (current-kill 0) date)))))))
  180. (provide 'org-datetree)
  181. ;; arch-tag: 1daea962-fd08-448b-9f98-6e8b511b3601
  182. ;;; org-datetree.el ends here