org-datetree.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; org-datetree.el --- Create Date entries in a tree
  2. ;; Copyright (C) 2009 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: 6.32trans
  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, the date tree will become a subtree under that entry, so the
  32. base level will be properly adjusted.")
  33. (defun org-datetree-find-date-create (date)
  34. "Find or create an entry for DATE."
  35. (let ((year (nth 2 date))
  36. (month (car date))
  37. (day (nth 1 date)))
  38. (org-set-local 'org-datetree-base-level 1)
  39. (widen)
  40. (goto-char (point-min))
  41. (when (re-search-forward "^[ \t]*:DATE_TREE:[ \t]+\\S-" nil t)
  42. (org-back-to-heading t)
  43. (org-set-local 'org-datetree-base-level
  44. (org-get-valid-level (funcall outline-level) 1))
  45. (org-narrow-to-subtree))
  46. (goto-char (point-min))
  47. (org-datetree-find-year-create year)
  48. (org-datetree-find-month-create year month)
  49. (org-datetree-find-day-create year month day)
  50. (goto-char (prog1 (point) (widen)))))
  51. (defun org-datetree-find-year-create (year)
  52. (let ((re "^\\*+[ \t]+\\([12][0-9][0-9][0-9]\\)[ \t\n]")
  53. match)
  54. (goto-char (point-min))
  55. (while (and (setq match (re-search-forward re nil t))
  56. (goto-char (match-beginning 1))
  57. (< (string-to-number (match-string 1)) year)))
  58. (cond
  59. ((not match)
  60. (goto-char (point-max))
  61. (or (bolp) (newline))
  62. (org-datetree-insert-line year))
  63. ((= (string-to-number (match-string 1)) year)
  64. (goto-char (point-at-bol)))
  65. (t
  66. (beginning-of-line 1)
  67. (org-datetree-insert-line year)))))
  68. (defun org-datetree-find-month-create (year month)
  69. (org-narrow-to-subtree)
  70. (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\)[ \t\n]" year))
  71. match)
  72. (goto-char (point-min))
  73. (while (and (setq match (re-search-forward re nil t))
  74. (goto-char (match-beginning 1))
  75. (< (string-to-number (match-string 1)) month)))
  76. (cond
  77. ((not match)
  78. (goto-char (point-max))
  79. (or (bolp) (newline))
  80. (org-datetree-insert-line year month))
  81. ((= (string-to-number (match-string 1)) month)
  82. (goto-char (point-at-bol)))
  83. (t
  84. (beginning-of-line 1)
  85. (org-datetree-insert-line year month)))))
  86. (defun org-datetree-find-day-create (year month day)
  87. (org-narrow-to-subtree)
  88. (let ((re (format "^\\*+[ \t]+%d-%02d-\\([01][0-9]\\)[ \t\n]" year month))
  89. match)
  90. (goto-char (point-min))
  91. (while (and (setq match (re-search-forward re nil t))
  92. (goto-char (match-beginning 1))
  93. (< (string-to-number (match-string 1)) day)))
  94. (cond
  95. ((not match)
  96. (goto-char (point-max))
  97. (or (bolp) (newline))
  98. (org-datetree-insert-line year month day))
  99. ((= (string-to-number (match-string 1)) day)
  100. (goto-char (point-at-bol)))
  101. (t
  102. (beginning-of-line 1)
  103. (org-datetree-insert-line year month day)))))
  104. (defun org-datetree-insert-line (year &optional month day)
  105. (let ((pos (point)))
  106. (skip-chars-backward " \t\n")
  107. (delete-region (point) pos)
  108. (insert "\n" (make-string org-datetree-base-level ?*) " \n")
  109. (backward-char 1)
  110. (if month (org-do-demote))
  111. (if day (org-do-demote))
  112. (insert (format "%d" year))
  113. (when month
  114. (insert (format "-%02d" month))
  115. (if day
  116. (insert (format "-%02d %s"
  117. day (format-time-string
  118. "%A" (encode-time 0 0 0 day month year))))
  119. (insert (format " %s"
  120. (format-time-string
  121. "%B" (encode-time 0 0 0 1 month year))))))
  122. (beginning-of-line 1)))
  123. (provide 'org-datetree)
  124. ;; arch-tag: 1daea962-fd08-448b-9f98-6e8b511b3601
  125. ;;; org-datetree.el ends here