Sunday, January 01, 2012

Nerd Food: Batch exports using latest org mode

I recently hit an annoying problem with org-mode: batch exports of gnuplot where failing with a mysterious error (full details here). To make a long story short, I was told to upgrade to latest org-mode from git. I followed the instructions on the brilliant org-mode FAQ and got there in no time.

But then it hit me, I needed to provided support for latest org mode in batch mode too! I tend to use default emacs settings in batch mode - well, lets put it this way, I never needed anything else - so I wasn't too sure how to do this. Last thing you need is your batch mode taking 1 minute to bootstrap because its running your full .emacs initialisation. And lord knows cunene ain't getting any smaller.

So after some fumbling I hacked myself a quick export.el that bootstraps a minimal latest org-mode:

(setq dotfiles-dir "~/.emacs.d/lisp")

(setq kill-buffer-query-functions
(remove 'process-kill-buffer-query-function
kill-buffer-query-functions))

(add-to-list 'load-path (concat dotfiles-dir "/other/gnuplot-mode"))
(add-to-list 'load-path (concat dotfiles-dir "/other/org-mode"))
(load-file (concat dotfiles-dir "/other/org-mode/lisp/org-install.el"))

(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(sh . t)
(gnuplot . t)
(R . t)))
(setq org-confirm-babel-evaluate nil)

(require 'org-install)
(require 'org)
You may notice the setq kill-buffer-query-functions bit. This was to stop a really annoying message coming from gnuplot:
Buffer "*gnuplot-version*" has a running process; kill it? (yes or no)
Googling didn't reveal any direct solutions to the problem, which is a bit worrying; I wonder if I'm getting this because I'm running latest gnuplot mode. At any rate, I managed to knocked up this hack based on some other process related problem and it made the problem go away. The full incantation for emacs is:
$ /usr/bin/emacs --batch --no-site-file --directory ${org_path}/lisp/ \
--directory ${org_path}/contrib/lisp/ \
--load export.el --visit ${target} --eval '(org-export-as-html-batch)'
Replacing ${org_path} with the path to your git clone of org mode and ${target} with the org file you want to generate HTML for.

1 comment:

Andrew said...

Thank you.

This was exactly what I need.

I did make some changes;

- I commented out everything with /other/ in it

- I added 'python' to the load-languages

- I used "org-latex-export-to-pdf" as my --eval '(...)' command

And it ran my embedded babel Python code, produced my plot, and made my PDF.

I couldn't be happier.