my-count-clocks
in the following Elisp section defines a function that counts clocks in the current org-mode section. It does not descent into sub-sections. You can easily modify my-count-clocks
to fit your special needs, e.g., work recursive on sub-sections.
That function has a predicate function as argument. The predicate gets a timestamp as argument and should return non-nil if that timestamp is to be counted.
An example for such a timestamp is given as my-pred
. my-pred
is non-nil for timestamps that tick at least 4 hours and shorter than 5 hours.
The other functions in the Elisp code are just helpers.
(defun org-element-parse-headline (&optional granularity visible-only)
"Parse current headline.
GRANULARITY and VISIBLE-ONLY are like the args of `org-element-parse-buffer'."
(let ((level (org-current-level)))
(org-element-map
(org-element-parse-buffer granularity visible-only)
'headline
`(lambda (el)
(and
(eq (org-element-property :level el) ,level)
(<= (org-element-property :begin el) (point))
(<= (point) (org-element-property :end el))
el))
nil 'first-match 'no-recursion)))
(defun my-count-clocks (pred)
"Counting clocks fulfilling PRED in current subsection."
(interactive "a Predicate:")
(let* ((headline (org-element-parse-headline))
(section (org-element-map
headline
'section
#'identity
nil 'first-match))
(clocks (org-element-map section 'clock (lambda (clock) (org-element-property :value clock))))
(num (cl-count-if pred clocks)))
(when (called-interactively-p 'any)
(message "Number of clocks: %d" num))
num))
(eval
`(defun decode-time-subtract (a b)
"Decode time difference between A and B."
(cl-mapcar #'-
(decode-time (time-subtract a b))
',(butlast (decode-time '(0 0)) 3))))
(defun my-pred (timestamp)
"Example predicate.
Non-nil for timestamp that clicks at least 4 hours and less than 5 hours."
(let ((b (org-timestamp--to-internal-time timestamp))
(e (org-timestamp--to-internal-time timestamp t)))
(cl-multiple-value-bind (sec minute hour day month year) (decode-time-subtract e b)
;; Put the condition of your choice here:
(and (eq year 0) (eq month 0) (eq day 0)
(>= hour 4)
(< hour 5))
)))
org-mode
tag, so I removed it. If it is relevant, please consider updating the question to show the relevance. – Drew Mar 10 '19 at 18:59