Org-mode PDF export failure

2010/10/26 Leave a comment

Having replaced my laptop due to a tea related accident, I am finding a few things that I need to reconfigure. Org exports are one of them. On my new fedora system, org-mode pdf exports fail with the messages:

LaTeX export done, pushed to kill ring and clipboard
Processing LaTeX file...
(Shell command failed with code 127 and some error output) [2 times]
Processing LaTeX file...done
org-export-as-pdf: PDF file was not produced

This can be fixed with:

yum install rubber

Categories: Emacs Tags: , ,


2010/10/12 Leave a comment

Devilspie is a wonderful tool that let’s you configure your feature free window manager as the buddah intended.

Here is my first foray into the world of optimising wimp window managers:

$ cat .devilspie/noborders.ds
(if (or (is (window_class) “Emacs”)
(is (window_class) “Gnome-terminal”))

Categories: Uncategorized

Minimal emacs config

2010/09/29 Leave a comment

After spilling tea on my laptop yesterday, I have been borrowing a friends thinkpad.  Here is the new emacs setup at the end of day one. I’ve only included the things that have actually annoyed me during the day at work.  I had to install slime, magit, swank-clojure and js2-mode using elpa.

(tool-bar-mode -1)
(menu-bar-mode -1)
(scroll-bar-mode -1)
(global-set-key (kbd "C-z") 'execute-extended-command)
(global-auto-revert-mode 1)
(ido-mode 1)
(global-set-key (kbd "C-x M-b") 'bury-buffer)
(global-set-key (kbd "C-n") 'next-logical-line)
(global-set-key (kbd "C-p") 'previous-logical-line)
(global-set-key (kbd "M-o") 'other-window)
(global-set-key (kbd "C-c C-g") 'rgrep)
(global-set-key (kbd "M-g") 'goto-line)
(global-set-key (kbd "M-(") 'insert-pair)
(global-set-key (kbd "M-[") 'insert-pair)
(global-set-key (kbd "C-M-{") 'insert-pair)
(global-set-key (kbd "M-\"") 'insert-pair)
(global-set-key (kbd "M-'") 'insert-pair)
(setq focus-follows-mouse t)
(setq mouse-autoselect-window t)
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt)
(fset 'yes-or-no-p 'y-or-n-p)
(mouse-avoidance-mode 'cat-and-mouse)
(put 'downcase-region 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'scroll-left 'disabled nil)
(put 'upcase-region 'disabled nil)
(winner-mode 1)
(global-unset-key "\C-x\C-c")
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ediff-window-setup-function (quote ediff-setup-windows-plain))
'(uniquify-buffer-name-style (quote forward) nil (uniquify)))
Categories: Emacs

Useful ssh settings

2009/09/22 Leave a comment

The control path settings will let you multiplex one ssh connection for multiple ssh sessions. Practically this means that you can ssh to a box, and scp will not ask you for a password when you copy a file to or from that box. Also, it’s useful for tab completion over the network.

Add this to your ~/.ssh/config file:

Compression yes

Host 192.168.*.*
        ControlPath ~/.ssh/master-%r@%h:%p
        ControlMaster auto
        ServerAliveInterval 30
        HashKnownHosts no

Host *
        ControlPath ~/.ssh/master-%r@%h:%p
        ControlMaster auto
        ServerAliveInterval 30
Categories: unix Tags: , ,

Maven completion in zsh

2009/09/22 2 comments

To get mvn completion in zsh, add this to your .zshrc:

function listMavenCompletions { reply=(cli:execute cli:execute-phase archetype:generate compile clean install test test-compile deploy package cobertura:cobertura jetty:run -Dmaven.test.skip=true -DarchetypeCatalog= -Dtest= `if [ -d ./src ] ; then find ./src -type f | grep -v svn | sed 's?.*/\([^/]*\)\..*?-Dtest=\1?' ; fi`); }
compctl -K listMavenCompletions mvn

It should complete the class names after ‘-Dtest=’, so that you can test one class at a time.

Categories: programming Tags: , ,

Strictly Fluent Java

2009/05/10 Leave a comment

Fluent APIs are interfaces that go out of their way to look like a domain language, or even conversational languages, such English. Common uses seem to be factories, and that’s what I’ve used them for most. Implementing these in languages like java will probably leave lots of odd looking methods littered about your classes, like:

  public FluentOrderFactory forCustomer(Customer customer)

because the common pattern for implementing this style of API is via method chaining on a single class:

public class FluentOrderFactory {
  public FluentOrderFactory static placeOrder() { /* snip */ }
  public FluentOrderFactory forCustomer(Customer customer) { /* snip */ }
  public FluentOrderFactory with(int quantity) { /* snip */ }
  public FluentOrderFactory of(Product product) { /* snip */ }
  public FluentOrderFactory and(int quantity) { /* snip */ }
  public FluentOrderFactory deliverTo(Address deliveryAddress) { /* snip */ }
  public FluentOrderFactory andSendinvoiceto(Address invoiceAddress) { /* snip */ }

So using static imports(a vital piece of all fluent developers’ toolkit), you can write:


but equally, you can write:


which makes no sense what-so-ever. And you wouldn’t know until it ran. Well, you might ;).

But in the client code of this factory, in your favourite IDE, you can easily make these sort of mistakes. It is simple enough to create an API that resembles English when read, but no programming language can have the flexibility of a spoken language. They just have to be so much more specific than the flexible world of human language. It is difficult to make use of pro-nouns for example. And even when you are given the restricted vocabulary of these classes, it is often difficult to guess (or even recall) the intent of some of these methods. You end up having to refer to other clients of the code, the most obvious example of which should be your unit tests. These APIs tend to be read-only ( – the opposite of Perl).

We can do better than this. By separating methods into separate classes, we can enforce a little order to this interface, and remove some of that confusing choice from each stage of the API.

public class FluentOrderFactory {
  public static CustomerStage placeOrder() { /* snip */ }
  public static class CustomerStage {
    public QuantityStage forCustomer(Customer customer) { /* snip */ }
  public static class QuantityStage {
    public ProductStage with(int quantity) { /* snip */ }
    public ProductStage and(int quantity) { /* snip */ }
	public InvoiceStage deliverTo(Address deliveryAddress) { /* snip */ }
  public static class ProductStage {
    public QuantityStage of(Product product) { /* snip */ }
  public static class InvoiceStage {
    public Order andSendinvoiceTo(Address invoiceAddress) { /* snip */ }

This limits the mistakes we can make to things like:


This is a little better. But there are still some holes. We can clean it up a little more if we remove the circular reference, and make that a list by using another Java 1.5 feature varargs.

public class FluentOrderFactory {
  public static CustomerStage placeOrder() { /* snip */ }
  public static class CustomerStage {
    public QuantityStage forCustomer(Customer customer, ProductQuantitiy... productQuantities) { /* snip */ }
  public static class ProductQuantitiy {
    public static ProductQuantitiy with(int quantity, Product product) { /* snip */ }
	public static ProductQuantitiy and(int quantity, Product product) { /* snip */ }
  public static class DeliveryStage {
	public InvoiceStage deliverTo(Address deliveryAddress) { /* snip */ }
  public static class InvoiceStage {
    public Order andSendinvoiceTo(Address invoiceAddress) { /* snip */ }

So now we say something like:

  placeOrder().forCustomer(bob, with(3, iPod), and(2, dvd)).deliverTo(deliveryAddress).andSendinvoiceTo(invoiceAddress);

There is something we lose here, and that is the auto-completion IDE hinting for some of the pieces of API. We have to hunt for the static factory method for the ProductQuantitiy class. And we can still mess up the use of “with” and “and”. But that doesn’t sound so likely.

After you’ve built a few of these things, you’ll start to notice a lot of duplicate code. In fact, a lot of the stages will be shared between the more complex objects. To reduce this duplication, we can apply liberal use of another of Java’s 1.5 features, generics, making our little stages look more like:

public abstract class AbstractStage {
  protected T next;
  protected String setter;
  protected AbstractStage(T n) {
    next = n;
  protected T getNextStage() {
      return next;

public class DeliveryStage extends AbstractStage {
  public DeliveryStage(T nextStage) {
  public T deliverTo(Address deliveryAddress) { /* snip */ }

/* snip */

public class DomainFactory {
  public static CustomerStage<deliveryStage> placeOrder() {
    return new CustomerStage(new DeliveryStage(new InvoiceStage(new Order())));

This factory method lets us specify the order of the stages we use for each class that we construct, and share any common methods. It does this at the cost of some readability for the factory, since there’s a little duplication, but if there are many shared phrases in your fluent API, that only differ on return types, then this can be quite an improvement. Also, that chain of generic classes is pretty impenetrable if you’re new to it, but at least the compiler will tell you when you’ve made a mistake.

You can even create a FluentApi class that contains methods like:

  public static  T and(T o) {
    return o;

that can be statically imported to provide a little grammatic sugar.

So really, a fluent API sounds like a nice thing to have in your code-base. And it is. But only in the more complex and prevalent areas of your code, because they add a fair amount of cognitive weight to the creation of objects, and if they are simple then you may well be better off with a “new” keyword. But, if you decide that you need the legibility in your domain, then they are an excellent way of making a start on the path to a domain specific language.

For simpler cases, stick to the separate classes, no generics style. It is easier to manage, and allows your tools to be more helpful. For the more complex cases, generics and reflection can save you from a lot of duplication, and makes you feel clever – which is the most important thing, after all.

Categories: programming Tags:

Maven, Junit4, Mockito and Anonymous classes

2009/04/10 Leave a comment

Mockito has a rather useful feature, answers, to allow you easily implement quick tests for objects that rely on behaviour of their dependencies:

when(mockFactory.createSomething()).thenAnswer(new Answer() { public Object answer(InvocationOnMock i) {
    return mockThing;

However, Junit4 fails to initalise the anonymous class and run any tests on it, reporting errors. There is an @org.unit.Ignore annotation that you can use if you are prepared to make the anonymous class a nested class, but I can’t work out where to put the annotation on an anonymous class. How irritating, and I guess I’m not the only one.

Categories: programming Tags: , ,


2009/04/08 Leave a comment

slashdot quote of the day: “I’m concerned that my blue-sky thinking will be obscured by your cloud computing.”

Categories: Uncategorized Tags:

Greg Young

2009/03/19 Leave a comment

I found this rather thought provoking … I shall be subscribing to his blog.

Categories: programming

My flymake + maven setup

2009/03/19 Leave a comment

In my day job, we use Maven2 to build our projects and I could not get used to netbeans or eclipse after years of perl and php in emacs (I’m sorry, but no matter how good the refactoring & debugging tools, their text editors are difficult to use and constantly getting in the way, and I really did try). So, I needed a way to run maven from emacs + flymake. I’d seen a few people document their setup, but this is how I got it working. It’s a bit hacky, and non-emacsy, cos it was easier to get going in shell than lisp. Clearly I am an emacs noob.

Since getting this up and running, a friend has pointed out jde-mvn which at some point I will try seriously and maybe even switch to … but until then – hey … I have my dodgy shell scripts.

So, the howto:

in your .emacs:

(require 'flymake-mvn)
(add-hook 'java-mode-hook 'flymake-java-mvn-mode-hook)

in your library-path, create a file (something like ~/emacs.d/flymake-maven.el):

(defun flymake-java-mvn-init ()
  "Return external compile process to run"
  (if jde-global-classpath
	  (list "flymake-maven" (list buffer-file-name (mapconcat 'identity jde-global-classpath ":")))
	(list "flymake-maven" (list buffer-file-name))))

(defun flymake-java-mvn-load ()
  "Setup keys and flymake error patterns"
  (push '(".+\\.java$" flymake-java-mvn-init) flymake-allowed-file-name-masks)
  (push '("^\\([^: \n]+\\):\\[\\([0-9]+\\),\\([0-9]+\\)] \\(.+\n?[^/]*?\n?[^/]*?\\)" 1 2 3 4) flymake-err-line-patterns)
  (push '("^warning \\([^: \n]+\\):\\[\\([0-9]+\\),\\([0-9]+\\)] \\(.+\\)" 1 2 3 4) flymake-err-line-patterns)
  (flymake-mode t)
  (local-set-key (kbd "C-c e") 'flymake-display-err-menu-for-current-line)
  (local-set-key (kbd "C-c n") 'flymake-goto-next-error)
  (local-set-key (kbd "C-c p") 'flymake-goto-prev-error)
  (local-set-key (kbd "C-c s") 'flymake-start-syntax-check)
  (local-set-key (kbd "C-c S") 'flymake-stop-all-syntax-checks)
  (local-set-key (kbd "C-c C-t") 'ed/java-mvn-run-unit-test)
  (local-set-key (kbd "C-c d") 'ed/java-mvn-debug-unit-test)
  (local-set-key (kbd "C-c b") 'jde-bug-set-breakpoint)
  (local-set-key (kbd "C-c c") 'jde-bug-clear-breakpoint)
  (local-set-key (kbd "C-c C-i") 'ed/java-indent-properties)
  (local-set-key (kbd "C-c C-.") 'jde-complete)
  (local-set-key (kbd "C-c C-/") 'semantic-complete-analyze-and-replace)
  (local-set-key (kbd "C-c C-m") 'tempo-snippets-clear-all))

(defun flymake-java-mvn-mode-hook ()
  "Initialise the java environment"
  (c-subword-mode 1)
  (setq indent-tabs-mode nil)
  (setq local-abbrev-table java-mode-abbrev-table)
  (abbrev-mode 1))

(provide flymake-mvn)

in your shell path (e.g. ~/bin/flymake-maven), create a file:


# find the pom for the specified file
if [ -f $1 ] ; then
	cd `echo $1 | sed 's/[^/]*$//'`
	if [ -d $1 ] ; then
		cd $1
while [ ! -f "pom.xml" ] ; do
	cd ..
	if [ "x/" = "x"`pwd -P` ] ; then
		echo "No pom.xml found"

# compile the class
if [ "x" != "x$2" -a -d target/classes -o -d build/classes ] ; then
	file=`echo $1 | sed -e 's?/?\\\\/?g' -e 's/\./\\./g'`
	awk_script='BEGIN { line = "" } { if ( $0 ~ /^ *\^/ ) { print line ; line = "" } else if ( $0 ~ /^'$file'/ ) { print line ; line = $0 } else { line = line " ; " $0 } } ; '
	javac -Xlint -classpath target/classes:target/test-classes:build/classes:build/test-classes:$2 -d target/classes $1 2>&1 | awk "$awk_script" | sed 's/  */ /g'
	mvn -fn -o -Dmaven.compiler.showWarnings=true compile test-compile | grep -v '\[INFO\]' | perl -p -e 's/(.)\n/$1 ; /gs' | perl -p -e 's/ +/ /g' | perl -p -e 's/\[WARNING\]/warning/' | sort -u

exit 0;

It takes an input of the file you’re editing, and works up the filesystem until it finds a ‘pom.xml’. It decides to call mvn or javac based on the existence of a target/classes or build/classes directory since maven will create the correct directory, and if it all goes wrong I can mvn clean to get back to a known good state. Then it takes the output from maven or javac, ignores all the info logging lines from maven, leaving (hopefully) lots of lovely compile errors in the file we were editing in one line per error, just as flymake expects.

For some reason, I haven’t reliably got maven to report warnings, but javac does that fine, so I just wait for the second compile, or hit C-c s to start flymake off to find out how many unchecked casts I have in todays code.

I Use C-c e to display the error message, C-c n to jump to the next error, and C-c p to get to the previous message.

I have keyboard shortcuts to run the unit tests, and at some point will make that commit to svn when they pass, perhaps with the trac ticket number I’m currently working on from org-mode as a comment … for another day, I feel 😉

The mvn-jde solution looks nicer, and as soon as I can prove that it works for all my day job projects, and I find the time to set it up, I’ll probably switch to that. But I bet maven3 comes out first 😉

Categories: Emacs, programming Tags: ,