worklog.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. (setq worklog-dir "~/Documents/journal/")
  2. (setq worklog-weeklies "~/Documents/journal/weeklies/")
  3. (setq worklog-seconds-in-a-day (* 24 60 60))
  4. (setq worklog-seconds-in-a-week (* 7 worklog-seconds-in-a-day))
  5. (defun filename-for-day (given-day)
  6. "Return filename for today's buffer."
  7. (concat worklog-dir
  8. (downcase (format-time-string "%a%m%d%y" given-day))
  9. ".org"))
  10. (defun filename-for-weekly (given-day)
  11. "Return filename for week's buffer."
  12. (concat worklog-weeklies
  13. (buffername-for-weekly given-day)))
  14. (defun buffername-for-weekly (given-day)
  15. (concat
  16. (downcase (format-time-string "week%U-%y" (find-day-in-week given-day 5)))
  17. ".org"))
  18. (defun filename-for-today ()
  19. (filename-for-day (current-time)))
  20. ;; unused!
  21. (defun filename-for-this-weekly ()
  22. (filename-for-weekly (current-time)))
  23. (defun filename-for-days-away-entry (days-away)
  24. "Return filename of a daily with int days-away offset."
  25. (concat worklog-dir
  26. (downcase
  27. (format-time-string
  28. "%a%m%d%y"
  29. (encode-time (decoded-time-add
  30. (decode-time nil nil t)
  31. (make-decoded-time :day days-away)))))
  32. ".org"))
  33. (defun yesterdays-daily ()
  34. "Open yesterday's news"
  35. (interactive)
  36. (find-file (filename-for-days-away-entry (- 1))))
  37. ;; unused!
  38. (defun read-last-week ()
  39. "Read the last seven day's entries into the current buffer."
  40. (interactive)
  41. (defun insert-filename-and-contents (fname)
  42. (goto-char (point-max))
  43. (newline)
  44. (insert "* ⋱ " (file-name-base fname))
  45. (newline)
  46. (insert-file-contents fname)
  47. (replace-regexp "[*]+ [^⋱].+" "*\\&"))
  48. (defun days-iter (current-day goal-day)
  49. (let ((todays-file (filename-for-days-away-entry current-day)))
  50. (if (> current-day goal-day)
  51. nil
  52. (progn (if (file-exists-p todays-file)
  53. (insert-filename-and-contents todays-file))
  54. (days-iter (+ current-day 1) goal-day)))))
  55. (days-iter (- 7) 0))
  56. (defun read-week-around-day (given-day)
  57. (defun insert-filename-and-contents (fname)
  58. (goto-char (point-max))
  59. (newline)
  60. (insert "* ⋱ " (file-name-base fname))
  61. (newline)
  62. (insert-file-contents fname)
  63. (replace-regexp "[*]+ [^⋱].+" "*\\&"))
  64. (defun insert-header-categories ()
  65. (goto-char (point-min))
  66. (insert "* TICKETS")
  67. (newline)
  68. (insert "* MEETINGS")
  69. (newline)
  70. (insert "* EXTRA CURRICULAR")
  71. (newline))
  72. (defun days-iter (weekday weekday-end)
  73. (let ((days-file (filename-for-day (find-day-in-week given-day weekday))))
  74. (cond ((> weekday weekday-end) nil)
  75. (t
  76. (progn
  77. (if (file-exists-p days-file)
  78. (insert-filename-and-contents days-file))
  79. (days-iter (+ weekday 1) weekday-end))))))
  80. (days-iter 1 5)
  81. (insert-header-categories))
  82. (defun worklog-load-notes-for-week (given-day)
  83. (find-file (filename-for-weekly given-day))
  84. (set-buffer (buffername-for-weekly given-day))
  85. (read-week-around-day given-day))
  86. (defun worklog-this-weeks-entry ()
  87. "Gather the notes from this week (to do on friday)."
  88. (interactive)
  89. (worklog-load-notes-for-week (current-time)))
  90. (defun a-week-ago-from-day (given-day)
  91. (time-add given-day (- worklog-seconds-in-a-week)))
  92. (defun worklog-last-weeks-entry ()
  93. "Gather the notes from last week (if u forgot to do it on friday)."
  94. (interactive)
  95. (worklog-load-notes-for-week (a-week-ago-from-day (current-time))))
  96. (defun worklog-todays-entry ()
  97. "Open the worklog buffer for today's date."
  98. (interactive)
  99. (find-file (filename-for-today)))
  100. (defun worklog-yesterdays-entry ()
  101. (interactive)
  102. (find-file (filename-for-days-away-entry (- 1))))
  103. (defun worklog-days-ago-entry (days-ago)
  104. (interactive "P")
  105. (find-file (filename-for-days-away-entry
  106. (- (prefix-numeric-value days-ago)))))
  107. (defun find-day-in-week (given-day day-of-week)
  108. "Return datetime that is 1,2,3,4,5:M,T,W,T,F in the same week as given-day."
  109. (let ((day (decode-time given-day)))
  110. (cond ((= (decoded-time-weekday day) day-of-week)
  111. given-day)
  112. ((< (decoded-time-weekday day) day-of-week)
  113. (find-day-in-week (time-add given-day worklog-seconds-in-a-day) day-of-week))
  114. ((> (decoded-time-weekday day) day-of-week)
  115. (find-day-in-week (time-add given-day (- worklog-seconds-in-a-day)) day-of-week)))))
  116. ;; unused!
  117. (defun this-weeks-friday ()
  118. (find-day-in-week (current-time) 5))
  119. ;;unused!
  120. (defun this-weeks-monday ()
  121. (find-day-in-week (current-time) 1))
  122. (defun worklog-parse-my-buffername (buffername)
  123. (let ((day-of-week (substring buffername 0 3))
  124. (month (string-to-number (substring buffername 3 5)))
  125. (day (string-to-number (substring buffername 5 7)))
  126. (year (string-to-number (substring buffername 7 9))))
  127. (encode-time (make-decoded-time :second 0 :minute 0 :hour 0
  128. :month month :day day :year year))))
  129. (defun worklog-date-for-current-buffer ()
  130. (worklog-parse-my-buffername (buffer-name)))
  131. (defun worklog-previous-daily ()
  132. (defun daily-exists-p (date)
  133. (file-exists-p (filename-for-day date)))
  134. (defun daily-iter (days-back current-daily-date)
  135. (let ((daily-date (time-add current-daily-date
  136. (- (* days-back worklog-seconds-in-a-day)))))
  137. (if (daily-exists-p daily-date)
  138. (find-file (filename-for-day daily-date))
  139. (daily-iter (1+ days-back) current-daily-date))))
  140. (interactive)
  141. (daily-iter 1 (worklog-date-for-current-buffer)))
  142. (defvar worklog-map (make-sparse-keymap)
  143. "Worklog bindings.")
  144. (global-set-key (kbd "C-c w") worklog-map)
  145. (define-key worklog-map (kbd "d") 'worklog-todays-entry)
  146. (define-key worklog-map (kbd "w") 'worklog-this-weeks-entry)
  147. (define-key worklog-map (kbd "p") 'worklog-previous-daily)
  148. (define-key worklog-map (kbd "y") 'worklog-yesterdays-entry)