Monday, March 11, 2013

Automatic bookmarking of locations in Emacs

One of the things I find myself doing over and again in Emacs is returning to the last place I was editing a file. Sometimes that's because I've killed a buffer before I should have done (thinking I was finished), sometimes it's because I've exited Emacs and sometimes it's because I simply return to something a day or two later.

Since it was incredibly annoying to have to search for the last place I was working I created a simple system in Emacs that automatically remembers where I was and then lets me jump there by hitting C-l (obviously you can choose whatever key binding you like, I choose that because I never use C-l for its real purpose).

The automatic bookmarking works by using three Emacs hooks: when kill-buffer-hook or after-save-hook are called it records the current line number of the buffer in an internal hash table; when kill-emacs-hook is called it makes sure that the line number in each current buffer is saved and serializes the hash table to ~/.emacs.d/last-location.

There are probably lots of improvements wizard elisp hackers can make, but here's what works for me (you need my-emacs-d to be the path to your .emacs.d directory):

PS As a commenter points out there's already saveplace.el that I had failed to find before writing my own.

Tuesday, March 05, 2013

A simple illustration of Go interfaces

Go's approach to object orientation is somewhat different to the familiar 'hierarchy of classes'. In place of classes with methods, Go has interfaces. An interface specifies a set of behaviors (functions that can be called). If a Go type has that interface (i.e. has all the functions specified in the interface) then it can be used wherever that interface is required.

A commonly used interface in Go is io.Writer. An io.Writer interface is defined as follows in the io package:
type Writer interface {
    Write(p []byte) (n int, err error)
Any user-defined type that has a function with signature Write(p []byte) (n int, err error) is an io.Writer and can be used wherever an io.Writer is needed. For example, the bufio package can be used to create a buffered version of any io.Writer by passing it to bufio.NewWriter.
func NewWriter(wr io.Writer) *Writer
bufio.NewWriter's argument is itself an interface (an io.Writer), so anything that has the Write(p []byte) (n int, err error) function can be passed to it. At the same time bufio.Writer (the type returned by bufio.NewWriter) is itself an io.Writer (because it too has the Write(p []byte) (n int, err error) function.

This style makes it possible to define other io.Writers that can be used with any type requiring an io.Writer. In one project I was working on it was helpful to have an io.Writer that kept count of how many bytes were written. Here's that package (called rather unimaginatively counter).

With that package any existing io.Writer could be wrapped with a call to counter.New() and the return result used as if it were the original io.Writer.

Monday, March 04, 2013

Some Pig

In the past I've written about writing. In my previous blog post I recommended three books:
On Writing Well, by William Zinsser. This book is an example of its own title. It's a delightfully well written book about writing that reads with the slippery ease of a Malcolm Gladwell. It's both enjoyable and informative. 
The AP Manual of Style. Although the AP book is about writing for newspapers it is full of useful advice about clarity. The fight for clarity is at the heart of non-fiction. Your goal is not to delight the reader with the breadth of your vocabulary, but to inform them about a subject in which you are claiming to be knowledgeable. News writing has to aim to be succinct, accessible and accurate; those are all good attributes for any non-fiction writing. 
The Elements of Style, by Strunk and White. The surprising thing about Strunk and White is that I can pick it up after years and years of owning it and rediscover its lessons. If you only buy one book from my list buy this one.
The fourth book I'd recommend isn't actually about writing, but reading it is a lesson in writing. The book is Charlotte's Web. If you've read or heard of Charlotte's Web you are likely wondering why I'm recommending children's fiction when the subject is writing. There's a simple reason.

Charlotte's Web was written by E. B. White: the White in Strunk and White.

If you read Charlotte's Web and think about the writing you'll notice that many of the lessons from The Elements of Style are clearly illustrated. The book is a pleasure to read as an adult despite being aimed at children.

The words are small and appropriate. The sentences clear and the prose flows easily. I think Charlotte's Web epitomizes a style that many writers should emulate no matter for whom they are writing. The actual subject of the book is quite difficult for children to digest, but White's writing makes them swallow a difficult tale with ease.

The clearest example of Strunk and White style is illustrated by the spider Charlotte. Writing in her web she's restricted by space and her own ability to form words. Yet she saves the pig's life by writing SOME PIG. Just two simple words express what's essential: this isn't just any pig. This is Wilbur.

When the reader first meets Charlotte she greets Wilbur with "Salutations". Wilbur doesn't understand the word and Charlotte is forced to explain:
"Salutations are greetings," said the voice. "When I say 'salutations', it's just my fancy way of saying hello or good morning. Actually, it's a silly expression, and I am surprised that I used it at all. [...]"
Charlotte seems to be echoing Strunk and White's command to 'Avoid fancy words'.

So, if you are writing something you hope others will read, remember the simplest of all writing rules: SOME PIG.