0

I would like to sum clock intervals from tasks by tag. I found this blog post (code below). When I use it, I get the same report, regardless of the tag I request:

Time: 4499:28 (4499 hours and 28 minutes)

Furthermore, I get this warning after evaluating the code:

‘org-re’ is an obsolete macro (as of Org 9.0); you can safely remove it.

The post is from 2008 and therefore I assume that the code is incompatible with my version.

How can I sum clocked intervals for a specific tag?

Appendix: here is the obsolete code:

(defun wicked/org-calculate-tag-time (matcher &optional ts te)
  "Return the total minutes clocked in headlines matching MATCHER.
MATCHER is a string or a Lisp form to be evaluated, testing if a
given set of tags qualifies a headline for inclusion. TS and TE
are time start (inclusive) and time end (exclusive). Call with a
prefix to be prompted for TS and TE.

For example, to see how much time you spent on tasks tagged as
URGENT, call M-x wicked/org-calculate-tag-time RET URGENT RET. To
see how much time you spent on tasks tagged as URGENT today, call
C-u M-x wicked/org-calculate-tag-time RET URGENT RET . RET +1 RET."
  (interactive (list
        (read-string "Tag query: ")
        (if current-prefix-arg (org-read-date))
        (if current-prefix-arg (org-read-date))))
  ;; Convert strings to proper arguments
  (if (stringp matcher) (setq matcher (cdr (org-make-tags-matcher matcher))))
  (if (stringp ts)
      (setq ts (time-to-seconds (apply 'encode-time (org-parse-time-string ts)))))
  (if (stringp te)
      (setq te (time-to-seconds (apply 'encode-time (org-parse-time-string te)))))
  (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
             (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
             (org-re
              "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:][email protected]:]+:\\)?[ \t]*$")))
     (case-fold-search nil)
         lspos
     tags tags-list tags-alist (llast 0) rtn level category i txt p
     marker entry priority (total 0))
    (save-excursion
      (org-clock-sum ts te)
      (goto-char (point-min))
      (while (re-search-forward re nil t)
    (catch :skip
      (setq tags (if (match-end 4) (match-string 4)))
      (goto-char (setq lspos (1+ (match-beginning 0))))
      (setq level (org-reduced-level (funcall outline-level))
        category (org-get-category))
      (setq i llast llast level)
      ;; remove tag lists from same and sublevels
      (while (>= i level)
        (when (setq entry (assoc i tags-alist))
          (setq tags-alist (delete entry tags-alist)))
        (setq i (1- i)))
      ;; add the nex tags
      (when tags
        (setq tags (mapcar 'downcase (org-split-string tags ":"))
          tags-alist
          (cons (cons level tags) tags-alist)))
      ;; compile tags for current headline
      (setq tags-list
        (if org-use-tag-inheritance
            (apply 'append (mapcar 'cdr tags-alist))
          tags))
      (when (and (eval matcher)
             (or (not org-agenda-skip-archived-trees)
             (not (member org-archive-tag tags-list))))
        ;; Get the time for the headline at point
        (goto-char (line-beginning-position))
        (setq total (+ total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)))
        ;; if we are to skip sublevels, jump to end of subtree
        (org-end-of-subtree t)))))
    (if (interactive-p)
    (let* ((h (/ total 60))
           (m (- total (* 60 h))))
      (message "Time: %d:%02d (%d hours and %d minutes)" h m h m)))
    total))
4
  • 1
    There are a couple of questions already answered about clocking tables and tags, like this or more probably this. I do understand that you're after a different use case, but further clarification will help.
    – Muihlinn
    May 27 '20 at 18:17
  • I agree with @Muihlinn: instead of showing what code does not work, you should show an example org file and what you expect to get.
    – NickD
    May 27 '20 at 18:31
  • The solution with the clock report in the second link (stackoverflow.com/questions/38545767/…) worked for me (#+BEGIN: clocktable :maxlevel 2 :scope file :tags "nicetag"). I can't mark it as a duplicate: would you like to write an answer? Thanks for the comment @NickD: next time I'll ask what I want to do first, and add an example of a way that failed after. May 28 '20 at 19:33
  • 1
    Yes, the "duplicate" is on SO. You can always write your own answer and accept it though (and a "link-only" answer might be acceptable in this case).
    – NickD
    May 28 '20 at 19:43
0

Following the link @Muihlinn's comment, one solution consists of a clock report:

#+BEGIN: clocktable :maxlevel 2 :scope file :tags "nicetag"
...
#+END:

I can't mark this question as a duplicate since the other solution is on a different forum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.