org-datetree.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. (defun org-datetree-find-date-create (date)
  29. "Find or create an entry for DATE."
  30. (let ((year (nth 2 date))
  31. (month (car date))
  32. (day (nth 1 date)))
  33. (org-datetree-find-year-create year)
  34. (org-datetree-find-month-create year month)
  35. (org-datetree-find-day-create year month day)
  36. (goto-char (prog1 (point) (widen)))))
  37. (defun org-datetree-find-year-create (year)
  38. (let ((re "^\\*+[ \t]+\\([12][0-9][0-9][0-9]\\)[ \t\n]")
  39. match)
  40. (goto-char (point-min))
  41. (while (and (setq match (re-search-forward re nil t))
  42. (goto-char (match-beginning 1))
  43. (< (string-to-number (match-string 1)) year)))
  44. (cond
  45. ((not match)
  46. (goto-char (point-max))
  47. (or (bolp) (newline))
  48. (org-datetree-insert-line year))
  49. ((= (string-to-number (match-string 1)) year)
  50. (goto-char (point-at-bol)))
  51. (t
  52. (beginning-of-line 1)
  53. (org-datetree-insert-line year)))))
  54. (defun org-datetree-find-month-create (year month)
  55. (org-narrow-to-subtree)
  56. (let ((re (format "^\\*+[ \t]+%d-\\([01][0-9]\\)[ \t\n]" year))
  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)) month)))
  62. (cond
  63. ((not match)
  64. (goto-char (point-max))
  65. (or (bolp) (newline))
  66. (org-datetree-insert-line year month))
  67. ((= (string-to-number (match-string 1)) month)
  68. (goto-char (point-at-bol)))
  69. (t
  70. (beginning-of-line 1)
  71. (org-datetree-insert-line year month)))))
  72. (defun org-datetree-find-day-create (year month day)
  73. (org-narrow-to-subtree)
  74. (let ((re (format "^\\*+[ \t]+%d-%02d-\\([01][0-9]\\)[ \t\n]" year month))
  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)) day)))
  80. (cond
  81. ((not match)
  82. (goto-char (point-max))
  83. (or (bolp) (newline))
  84. (org-datetree-insert-line year month day))
  85. ((= (string-to-number (match-string 1)) day)
  86. (goto-char (point-at-bol)))
  87. (t
  88. (beginning-of-line 1)
  89. (org-datetree-insert-line year month day)))))
  90. (defun org-datetree-insert-line (year &optional month day)
  91. (let ((pos (point)))
  92. (skip-chars-backward " \t\n")
  93. (delete-region (point) pos)
  94. (insert "\n* \n")
  95. (backward-char 1)
  96. (if month (org-do-demote))
  97. (if day (org-do-demote))
  98. (insert (format "%d" year))
  99. (when month
  100. (insert (format "-%02d" month))
  101. (if day
  102. (insert (format "-%02d %s"
  103. day (format-time-string
  104. "%A" (encode-time 0 0 0 day month year))))
  105. (insert (format " %s"
  106. (format-time-string
  107. "%B" (encode-time 0 0 0 1 month year))))))
  108. (beginning-of-line 1)))
  109. (provide 'org-datetree)
  110. ;; arch-tag: 1daea962-fd08-448b-9f98-6e8b511b3601
  111. ;;; org-datetree.el ends here