diff --git a/org-clock-agg.el b/org-clock-agg.el index 7eb1f91..448b01d 100644 --- a/org-clock-agg.el +++ b/org-clock-agg.el @@ -29,6 +29,67 @@ ;;; Code: (require 'compat) +(require 'org) +(require 'org-ql) + +;; Reset org-ql cache +(setq org-ql-cache (make-hash-table :weakness 'key)) + +(defun org-clock-agg--parse-clocks (headline) + (let ((contents (buffer-substring-no-properties + (org-element-property :contents-begin headline) + (org-element-property :contents-end headline)))) + (with-temp-buffer + (insert contents) + (let (res) + (org-element-map (org-element-parse-buffer) 'clock + (lambda (clock) + (let ((start ) + (end (time-convert + (org-timestamp-to-time (org-element-property :value clock) t) + 'integer)))) + (push + `((:start . ,(time-convert + (org-timestamp-to-time (org-element-property :value clock)) + 'integer)) + (:end . ,(time-convert + (org-timestamp-to-time (org-element-property :value clock) t) + 'integer))) + res)) + nil nil 'headline) + res)))) + +(defun org-clock-agg--parse-headline () + (let* ((headline (org-element-headline-parser)) + (tags-val (org-ql--tags-at (point))) + (tags (seq-filter + #'stringp ;; to filter out `org-ql-nil' + (append (unless (eq (car tags-val) 'org-ql-nil) + (car tags-val)) + (unless (eq (cdr tags-val) 'org-ql-nil) + (cdr tags-val))))) + (file (buffer-file-name)) + (outline-path (mapcar + #'substring-no-properties + (org-ql--outline-path))) + (category (org-get-category))) + `((:headline . ,headline) + (:tags . ,tags) + (:file . ,file) + (:outline-path . ,outline-path) + (:category . ,category) + (:clocks . ,(org-clock-agg--parse-clocks headline))))) + +(defun org-clock-agg--query (from to files) + (org-ql-query + :select #'org-clock-agg--parse-headline + :from files + :where `(clocked :from ,from :to ,to))) + + +(defun org-clock-agg () + (interactive) + ()) (provide 'org-clock-agg) ;;; org-clock-agg.el ends here