0

I grabbed code from org effectiveness as shown below:

(defun org-effectiveness-count-todo()       ;;org-effectiveness-count-todo
  "Print a message with the number of todo tasks in the current buffer"
  (interactive)
  (save-excursion 
    (goto-char (point-min))
    (message "Number of TODO: %d" (count-matches "TODO"))))

When I put org-effectiveness-count-todo in any buffer, it shows TODO count correctly. When I put this code in agenda header, it shows only zero.

(org-agenda-overriding-header (org-effectiveness-count-todo) ...

image1[![][1]]2

Drew
  • 75,699
  • 9
  • 109
  • 225
nirag
  • 43
  • 5
  • For a function to return a result, the item to be returned is generally placed at the end of where the function exits. You have placed a message at the end of your function, instead of the value you desire. I don't know if that is the only problem, but it is certainly one of the problems. To make pretty code on stackexchange, please indent four spaces (see revised example above). – lawlist May 10 '18 at 22:18
  • Here is an example of returning a result at the point where the function exits: `(defun my-function () "Doc-string." (interactive) (let (result) (save-excursion (goto-char (point-min)) (setq result (count-matches "TODO"))) (message "Number of TODO: %d" result) result))` As mentioned above, I do not know if that was your own problem, but it was definitely one of them. – lawlist May 10 '18 at 22:25
  • FYI: If the agenda header is drawn *before* the buffer is fully populated, then there would be no reason to count matches in a buffer that does not yet have enough of the data to produce the correct result. You'll need to see where it is in the series of events that the header is created using the `org-agenda-overriding-header` value and do your calculations before that point in time. It may be helpful to also look for when the function `org-agenda-format-item` is called because that is one of the last places data is modified before the buffer is populated; e.g., map through data near it. – lawlist May 10 '18 at 22:42
  • Here is a link to a related thread where the author (who wrote the answer) chose to wait until the buffer is populated and then perform the count, and then modify the header with the count: https://emacs.stackexchange.com/a/27424/2287 . That is not as elegant as combing through the data before the buffer is populated and placing the desired header from the getgo, but after the fact is nevertheless a possibility you may wish to pursue. – lawlist May 10 '18 at 23:59
  • @lawlist, appreciate your efforts , – nirag May 11 '18 at 00:01
  • I went through this answer but couldn't understand, thought there would be simpler workaround but have to go through learning curve – nirag May 11 '18 at 00:04
  • The problem is not really understanding the code so much as the fact that the relevant functions are super lengthy and there is no hook to access the data before the header is created in the agenda buffer. In a perfect world, there would be a hook right *after* `(setq rtnall (append rtnall rtn))` in `org-search-view`, `org-todo-list`, and `org-tags-view`. Just after that section is where the header is inserted into the agenda buffer. I am the only person on the face of the earth who believes it is okay to modify the heck out of Emacs -- everyone else bends over backwards to use `advice`. – lawlist May 11 '18 at 00:45

0 Answers0