2020-01-06 Org agenda statistics, part I

Some time ago I wrote about the org-batch-agenda macro. I decided to finally put it in good use. My goal is to write a command-line tool (or alternatively, an Emacs command) displaying some statistics about my pending tasks.

However, for that I needed to solve two (minor) problems. The first one is my Org-mode configuration. I didn’t want to load all my init.el when launching Emacs in a shell script (it takes about 15 seconds for my Emacs to start), and emacs -Q is not good, either, since my Org-mode (including the agenda) is heavily customized. The solution is quite obvious – I am going to put all my Org-mode-related settings in another file and just load it in my init.el. This way, I could use emacs -Q --load=~/.emacs.d/org-init.el to load just my settings and have Emacs start in under a second. Even better, I can prepare another start file, which will just load org-init.el and run org-batch-agenda with suitable parameters – in my case, I went with (org-batch-agenda-csv "a" org-agenda-span 'day). This way I could omit all the quoting problems with calling Elisp from the command line.

All this is a bit easier said than done. For instance, I have a few Org-related settings which do not need to be loaded in this scenario, like org-mru-clock. Also, I decided to kick (setq org-clock-persist 'history) out of org-init.el, since it made the Emacs print stuff like

Restoring clock data
Loading /home/mbork/.emacs.d/org-clock-save.el (source)...

to stderr, and while redirecting stderr to /dev/null is not hard, I didn’t really want extra steps.

Anyway, this is the first part. The second one is taking the agenda for today in csv format and do something useful with it. A natural solution would be probably to put the results of running org-batch-agenda into some buffer and use Elisp to analyze the resulting csv. This looks promising, but has some issues. One of them is that org-batch-agenda sends its output to standard-output. This is an Emacs concept, much similar to Unix’s stdout. By default, it is nil, which means that the output lands in the echo area – or on the real stdout if Emacs is launched in batch mode. (A minor gripe is that somehow org-batch-agenda seems to put an extra newline on stderr. I looked at its source, but I have no idea why it happens. So it looks like I am going to redirect stderr to /dev/null after all.)

A worse thing is how org-batch-agenda works under the hood: it just runs the agenda as usual and scans the created buffer to get all the info, then outputs it as csv. The problem here is that it is apparently only meant to be run in Emacs batch mode, since it does all the things normally done by the org-agenda command, like creating the agenda buffer and window and switching to it.

So, it seems that after all these preparations I can just say emacs -Q --script ~/.emacs.d/org-init.el 2> /dev/null, and get a nice csv agenda on stdout. I can now do all sorts of nice stuff with it. Some of my ideas are: counting the overdue tasks (so that I could beemind their number), randomly selecting an overdue task to work on, or prepare a simple web-based interface so that I can put Emacs on a server and look at my agenda for the day from my smartphone.

(to be continued)

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode