org-datetree.el 6.5 KB

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