3

Is there any way to enable clockreport-mode in a TODO-agenda view? I mainly use this view because I don't really set dates for my tasks so the agenda-view is not really helpful for me (I'm using spacemacs by the way).

Anyway the clockreport-mode only works in 'agenda' - Agenda-Buffers. Otherwise it will throw an error Not allowed in todo-type agenda buffers.

The function in org-agenda.el is defined like this:

    (defun org-agenda-clockreport-mode ()   
    "Toggle clocktable mode in an agenda buffer."   
    (interactive)   
    (org-agenda-check-type t 'agenda) 
    (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))   
    (org-agenda-set-mode-name)   
    (org-agenda-redo)   
    (message "Clocktable mode is %s"
           (if org-agenda-clockreport-mode "on" "off")))

And I think this is where the clocktable gets inserted in defun-org-agenda-list

      (when (and org-agenda-clockreport-mode clocktable-start)
    (let ((org-agenda-files (org-agenda-files nil 'ifmode))
          ;; the above line is to ensure the restricted range!
          (p (copy-sequence org-agenda-clockreport-parameter-plist))
          tbl)
      (setq p (org-plist-delete p :block))
      (setq p (plist-put p :tstart clocktable-start))
      (setq p (plist-put p :tend clocktable-end))
      (setq p (plist-put p :scope 'agenda))
      (setq tbl (apply 'org-clock-get-clocktable p))
      (insert tbl)))

But as im still an Elisp novice I don't really know what I can do to enable and display a clocktable in a todo-agenda Buffer.

Edit:

Thanks to Tobias answer I found a way to work around the problem so that it fits my use case. This avoids using org-agenda-clockreport-mode, so I guess it doesn't really answer the question but it's good enough for me right now.

I took the clocktable code from org-agenda.el and modified it slightly to turn it into my own function that can insert a clocktable.

(defun own-clock-report ()
  "function to insert clocktable"
(goto-char (point-max))
(let ((org-agenda-files (org-agenda-files nil 'ifmode))
      ;; the above line is to ensure the restricted range!
      (p (copy-sequence org-agenda-clockreport-parameter-plist))
      tbl)
  (setq p (org-plist-delete p :block))
  (setq p (plist-put p :tstart "<today>"))
  (setq p (plist-put p :tend "<now>"))
  (setq p (plist-put p :scope 'agenda))
  (setq tbl (apply 'org-clock-get-clocktable p))
  (open-line 1)
  (insert tbl)))

Then I used a hook to add it to my agenda view: (add-hook 'org-agenda-finalize-hook #'own-clock-report)

Using org-agenda-mode-hook seems to work as well, if you like the clockreport on top of the agenda.

The only drawback this method has is, that every time I change the Todo-state of an item or clock in/out of it, a clocktable appears below the item and I have to rebuild the agenda buffer to make it disappear.

6
  • BTW: Nicely prepared question +1!
    – Tobias
    Dec 10, 2019 at 15:06
  • I tried to fix my solution at the weekend but it turned out that the situation is much more kinky than I thought. I deleted my answer. As reparation I put a bounty of 50pts on the question. Hope that helps to draw attention of people that directly work with org-agenda.
    – Tobias
    Dec 16, 2019 at 7:43
  • I didn't delete my answer but marked it prominently as nonworking. I think it is valuable information for others trying to answer your question.
    – Tobias
    Dec 16, 2019 at 7:59
  • @Tobias With your answer I was able to find a workaround. This doesn't really solve the problem of using org-agenda-clockreport-mode in todo-agenda buffers though.
    – captainh2o
    Dec 17, 2019 at 14:35
  • If you don't want the clock table to appear every time you make a change, it seems that a way to do it would be to bind the function to a shortcut, and run it manually, instead of binding it to org-agenda-finalize-hook?
    – xji
    Apr 4, 2020 at 22:04

1 Answer 1

1

This proposed answer does not work! (See the comments.) I leave it here as warning for others trying to answer this question.

(Please keep the reputation of this answer at zero level. It does not an answer the question so it does not deserve a positive reputation but I definitively consider it valuable information for others trying so I don't think that it deserves a negative reputation.)

Adding org-agenda-clockreport-mode to any hook of org-agenda-mode is a bit tricky since org-agenda-clockreport-mode wants the agenda buffer fully built when it runs.

Adding org-agenda-clockreport-mode directly to org-agenda-mode-hook or org-agenda-finalize-hook does not work.

But we can suspend org-agenda-clockreport-mode until the agenda command is finished with post-command-hook:

(defun my-org-agenda-clockreport ()
  "Start `org-agenda-clockreport-mode' from `post-command-hook'."
  (remove-hook 'post-command-hook #'my-org-agenda-clockreport)
  (org-agenda-clockreport-mode))

(defun my-org-agenda-hook-fun ()
  "Start clockreport through `org-agenda-finalize-hook'."
  (add-hook 'post-command-hook #'my-org-agenda-clockreport))

(add-hook 'org-agenda-finalize-hook #'my-org-agenda-hook-fun)
4
  • Unfortunately it throws an error if I use those two functions: Error in post-command-hook (my-org-agenda-clockreport): (error "Not allowed in todo-type agenda buffers") It only works in agenda-type agenda buffers and there it has another issue: whenever I press a key to navigate around the agenda buffer it toggles the org-agenda-clockreport-mode on or off, depending on the current state.
    – captainh2o
    Dec 12, 2019 at 16:10
  • I'm on org-mode 9.2.5 btw.
    – captainh2o
    Dec 12, 2019 at 16:18
  • @captainh2o You should not use those two functions by yourself. They should only be called through org-agenda-finalize-hook. I just had a look. org-agenda-finalize-hook is run more often than I thought. Can you please substitute it by org-agenda-mode-hook and try again? Make sure that org-agenda-finalize-hook does not contain my-org-agenda-hook-fun anymore. Maybe apply remove-hook or better just restart Emacs.
    – Tobias
    Dec 12, 2019 at 17:54
  • "Use" was a bit of a misnomer. Adding it to my config is what I meant. After replacing the hook with org-agenda-mode-hook the behavior doesn't change though (org-agenda-finalize-hook is empty). It still shows the post-command-hook error in todo-type agenda buffers. And in agenda-type agenda buffers it also keeps toggling the clockreport-mode (the agenda is being rebuilt with every navigating command - unless I switch to another buffer and back to the agenda buffer)
    – captainh2o
    Dec 13, 2019 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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