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"
    (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) ...


  • 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: 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 0:01

Your Answer

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

Browse other questions tagged or ask your own question.