From 37fe64b4d8023020548cb580a3bccc97e142f137 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Tue, 13 Dec 2022 07:19:41 -0800 Subject: [PATCH] [oni-org] Add some Pomodoro[1] helper functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ‘oni-org-increment-property’: A generic command that increments a numeric org property - ‘oni-org-pomodoro-add-note’: A command that adds a note to the org item logbook that states a pomodoro was completed. - ‘oni-org-pomodoro-times’: A function that parses an org item logbook to figure out at what times a pomodoro had been completed for that particular task. - ‘oni-org-pomodoro-times-for-date’: A function that finds the times a pomodoro has been marked as completed for a specified date. - ‘oni-org-archive-old-tasks’: Unrelated to the other functions, just a command that archives all of the tasks that have been closed in the previous month. [1]: https://francescocirillo.com/products/book-the-pomodoro-technique --- oni-org/oni-org.el | 54 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/oni-org/oni-org.el b/oni-org/oni-org.el index 242cc32..3ce98b4 100644 --- a/oni-org/oni-org.el +++ b/oni-org/oni-org.el @@ -958,7 +958,8 @@ placed above TARGET. Otherwise it will be placed below it." (let ((components (org-heading-components))) (list (cons 'done (and (org-entry-is-done-p) t)) (cons 'title (nth 4 components)) - (cons 'poms (string-to-number (or (alist-get "POMS" (org-entry-properties) nil nil #'string=) "0"))) + (cons 'poms (length (oni-org-pomodoro-times-for-date + (org-parse-time-string date)))) (cons 'id (org-id-get-create))))) (format "TODO=\"NEXT\"|CLOSED>=\"<%s 0:00>\"+CLOSED<=\"<%s 23:59>\"" date date) @@ -991,5 +992,56 @@ placed above TARGET. Otherwise it will be placed below it." (defalias 'org-dblock-write:oni-pomodoro-overview 'oni-org-dblock-write-pomodoro-overview) (org-dynamic-block-define "oni-pomodoro-overview" #'oni-org-insert-pomodoro-overview) +(defun oni-org-increment-property (property) + (interactive + (list (org-read-property-name))) + (let ((value (string-to-number (or (org-entry-get (point) property) "0")))) + (org-set-property property (number-to-string (1+ value))))) + +(add-to-list 'org-log-note-headings + (cons 'pomodoro "Finished a pomodoro on %t")) + +(defun oni-org-pomodoro-add-note () + (interactive) + (org-add-log-setup 'pomodoro nil nil 'time)) + +(defun oni-org-pomodoro-times () + (let ((entry-beginning (org-entry-beginning-position)) + (entry-end (org-entry-end-position)) + results) + (save-excursion + (goto-char entry-beginning) + (save-match-data + (re-search-forward org-logbook-drawer-re entry-end) + (let ((logbook-beginning (match-beginning 0)) + (logbook-end (match-end 0))) + (goto-char logbook-beginning) + (while (re-search-forward + (rx "Finished a pomodoro on " + (group "[" (one-or-more (not "]")) "]")) + logbook-end t) + (push (org-parse-time-string (match-string 1)) results))))) + results)) + +(defun oni-org-pomodoro-times-for-date (date) + (let ((day (decoded-time-day date)) + (month (decoded-time-month date)) + (year (decoded-time-year date))) + (seq-filter (lambda (time) (and (= (decoded-time-day time) day) + (= (decoded-time-month time) month) + (= (decoded-time-year time) year))) + (oni-org-pomodoro-times)))) + +;;; Archive Management + +(defun oni-org-archive-old-tasks () + "Archive tasks in all agenda files that were closed before the current month." + (interactive) + (let ((match (format-time-string "CLOSED<\"<%Y-%m-01>\""))) + (mapc (lambda (p) + (goto-char p) + (org-archive-subtree)) + (reverse (org-map-entries #'point match 'agenda))))) + (provide 'oni-org) ;;; oni-org.el ends here