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: , ,

Devilspie

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”))
(undecorate))

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)
(display-time)
(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
;; 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=http://tapestry.formos.com/maven-snapshot-repository -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:

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

but equally, you can write:

  with(2).placeOrder().deliverTo(invoiceAddress).of(iPod).andSendinvoiceTo(deliveryAddress).and(3);

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:

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

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) {
    super(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) {
    someWork();
    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: , ,
Follow

Get every new post delivered to your Inbox.