+org-daypage.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;; org-daypage.el --- Org-Mode Day Page.
  2. ;;
  3. ;; Copyright (C) 2010-2011 Thomas Parslow
  4. ;;
  5. ;; Author: Thomas Parslow <tom@almostobsolete.net>
  6. ;; Created: June, 2010
  7. ;; Version: 1
  8. ;; Keywords: orgmode, daypage
  9. ;;; License
  10. ;;
  11. ;; This program is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;;
  16. ;; This program is distributed in he hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. ;;
  24. ;;; Installation
  25. ;;
  26. ;; Add org-daypage.el to your load path and add (require 'org-daypage)
  27. ;; to your .emacs.
  28. ;;; Configuration:
  29. ;;
  30. ;; No keys are defined by default. So you may wish to add something
  31. ;; like the following to your .emacs as well:
  32. ;;
  33. ;; (define-key daypage-mode-map (kbd "<C-left>") 'daypage-prev)
  34. ;; (define-key daypage-mode-map (kbd "<C-right>") 'daypage-next)
  35. ;; (define-key daypage-mode-map (kbd "<C-up>") 'daypage-prev-week)
  36. ;; (define-key daypage-mode-map (kbd "<C-down>") 'daypage-next-week)
  37. ;; (define-key daypage-mode-map "\C-c." 'daypage-time-stamp)
  38. ;;
  39. ;; (global-set-key [f11] 'todays-daypage)
  40. ;; (global-set-key [f10] 'yesterdays-daypage)
  41. ;; (global-set-key "\C-con" 'todays-daypage)
  42. ;; (global-set-key "\C-coN" 'find-daypage)
  43. (eval-when-compile (require 'cl))
  44. (setq daypage-path "~/notes/days/")
  45. (defvar daypage-mode-map
  46. (let ((map (make-sparse-keymap)))
  47. map)
  48. "The key map for daypage buffers.")
  49. (defun find-daypage (&optional date)
  50. "Go to the day page for the specified date, or todays if none is specified."
  51. (interactive (list
  52. (org-read-date "" 'totime nil nil
  53. (current-time) "")))
  54. (setq date (or date (current-time)))
  55. (find-file (expand-file-name (concat daypage-path (format-time-string "%Y-%m-%d" date) ".org"))))
  56. (defun daypage-p ()
  57. "Return true if the current buffer is visiting a daypage"
  58. (if (daypage-date)
  59. t
  60. nil))
  61. (defun daypage-date ()
  62. "Return the date for the daypage visited by the current buffer
  63. or nil if the current buffer isn't visiting a dayage"
  64. (let ((file (buffer-file-name))
  65. (root-path (expand-file-name daypage-path)))
  66. (if (and file
  67. (string= root-path (substring file 0 (length root-path)))
  68. (string-match "\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\).org$" file))
  69. (flet ((d (i) (string-to-number (match-string i file))))
  70. (encode-time 0 0 0 (d 3) (d 2) (d 1)))
  71. nil)))
  72. (defun maybe-daypage ()
  73. "Set up daypage stuff if the org file being visited is in the daypage folder"
  74. (let ((date (daypage-date)))
  75. (when date
  76. ; set up the daypage key map
  77. (use-local-map daypage-mode-map)
  78. (set-keymap-parent daypage-mode-map
  79. org-mode-map)
  80. (run-hooks 'daypage-hook))))
  81. (add-hook 'org-mode-hook 'maybe-daypage)
  82. (defun daypage-next ()
  83. (interactive)
  84. (find-daypage
  85. (seconds-to-time (+ (time-to-seconds (daypage-date))
  86. 86400)))
  87. (run-hooks 'daypage-movement-hook))
  88. (defun daypage-prev ()
  89. (interactive)
  90. (find-daypage
  91. (seconds-to-time (- (time-to-seconds (daypage-date))
  92. 86400)))
  93. (run-hooks 'daypage-movement-hook))
  94. (defun daypage-next-week ()
  95. (interactive)
  96. (find-daypage
  97. (seconds-to-time (+ (time-to-seconds (daypage-date))
  98. (* 86400 7))))
  99. (run-hooks 'daypage-movement-hook))
  100. (defun daypage-prev-week ()
  101. (interactive)
  102. (find-daypage
  103. (seconds-to-time (- (time-to-seconds (daypage-date))
  104. (* 86400 7))))
  105. (run-hooks 'daypage-movement-hook))
  106. (defun todays-daypage ()
  107. "Go straight to todays day page without prompting for a date."
  108. (interactive)
  109. (find-daypage)
  110. (run-hooks 'daypage-movement-hook))
  111. (defun yesterdays-daypage ()
  112. "Go straight to todays day page without prompting for a date."
  113. (interactive)
  114. (find-daypage
  115. (seconds-to-time (- (time-to-seconds (current-time))
  116. 86400)))
  117. (run-hooks 'daypage-movement-hook))
  118. (defun daypage-time-stamp ()
  119. "Works like (and is basically a thin wrapper round)
  120. org-time-stamp except the default date will be the date of the daypage."
  121. (interactive)
  122. (unless (org-at-timestamp-p)
  123. (insert "<" (format-time-string "%Y-%m-%d %a" (daypage-date)) ">")
  124. (backward-char 1))
  125. (org-time-stamp nil))
  126. (defun daypage-new-item ()
  127. "Switches to the current daypage and inserts a top level heading and a timestamp"
  128. (interactive)
  129. (todays-daypage)
  130. (end-of-buffer)
  131. (if (not (bolp))
  132. (insert "\n"))
  133. (insert "* <" (format-time-string "%Y-%m-%d %a" (daypage-date)) "> "))
  134. (provide 'org-daypage)