[I] am rarely happier than when spending an entire day programming my computer to perform automatically a task that it would otherwise take me a good ten seconds to do by hand.
The next few chapters will focus on calculations in Emacs Lisp. In particular, we’ll calculate the amount of thread needed to purchase for any given cross stitch pattern.
First of all, let’s get some context by taking a closer look at a pattern.
Open up the coe‑xx/game‑and‑watch.xx
file.
Turn on M‑x coe‑xx‑mode
.
You should see the classic silhouette of Mr Game and Watch.
This tiny, blocky pattern is a fairly easy initiation into cross-stitching.
Use M‑x normal‑mode
to revert back to the text representation.
Take a look at the legend:
LEGEND a - black - 4 strands
Each type of stitch is made with a coloured thread, a number of strands of which are threaded through the needle. Four strands will give a thick bold stitch, whereas a single strand will likely show the cloth behind. Unsurprisingly, a four-strand stitch will also need four times as much thread.
Reading the legend, this pattern only has a single type of stitch
with the colour code black
that
is 4 strands thick. It’s keyed by
a
, so all
a
s in the pattern will be
stitched as such.
If we estimate the length to make a single-strand stitch, we can work out the total length of a given colour simply by looking at the pattern and doing some arithmetic.
Purchasing it is a bit more nuanced than you’d expect. Embroidery thread is always six strands thick, sold in a bundle known as a skein. When stitching, an artist cuts an armful of thread from a skein and separates out the strands they need.
Our goal is to calculate the number of skeins required to make this pattern, and in doing so calculate the length of thread.
If that seems too complex, don’t panic! You won’t need to do any calculations by hand — we’ll code the arithmetic up in Emacs instead.
Open up the demo with M‑x coe‑demo‑xx
.
Steps
The demo does a few things:
It creates a
*xx*
buffer and writes some text to it.It asks for the number of strands to use.
It asks for the number of stitches of the desired thread colour in the pattern.
It writes these to the buffer.
It calculates the number of skeins to purchase.
For the moment, we’re going to manually count the number of stitches of a given colour. But you can probably see where this is going: once we know enough Elisp, we’ll get Emacs to do the counting for us.
Coding M‑x xx
will give us an understanding of input handling,
arithmetic and a fair amount of Emacs Lisp experience. It will
introduce us to the bread and butter of the
interpreter: s-expressions and special forms.
In writing it, you will learn how to experiment and troubleshoot with
these valuable tools.
Learning objectives
Identify the different parts of an Emacs Lisp s-expression.
Understand how the Emacs Lisp interpreter reads and evaluates code.
Identify special forms.
Learn the evaluation strategy of the
defun
andlet
special forms.Identify the different parts of an Emacs Lisp expression.
Define local variables with
let
.Ask for user input with
read
.Use Elisp to perform arithmetic calculations .
Manipulate buffers and text through code.
Debug code using the
*scratch*
buffer.