<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Wilfred Hughes::Blog</title>
    <description>programming, language design, and human factors</description>
    <link>https://www.wilfred.me.uk/</link>
    <atom:link href="https://www.wilfred.me.uk/rss.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Difftastic, the Fantastic Diff</title>
        <description>&lt;p&gt;I’ve always wanted a structural diff tool, so I built
&lt;a href=&quot;https://github.com/wilfred/difftastic&quot;&gt;difftastic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/difftastic_demo.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This has been the most fascinating, most frustrating, and most
challenging program I’ve ever written.&lt;/p&gt;

&lt;h2 id=&quot;how-hard-could-it-be&quot;&gt;How Hard Could It Be?&lt;/h2&gt;

&lt;p&gt;If you write Lisp code for a while, you start to see code like
JSON. Everything is basically a list.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/json_diff.png&quot; /&gt;
    &lt;figcaption&gt;json-diff example&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/andreyvit/json-diff&quot;&gt;json-diff&lt;/a&gt; already exists,
and it’s pretty good. I wanted something similar for programming
languages.&lt;/p&gt;

&lt;p&gt;After a huge amount of experimentation, I have something that
works. In this post, I’ll show you how it works.&lt;/p&gt;

&lt;p&gt;I won’t show the many, many dead ends and failed designs along the
way. We can pretend that I got it right first time.&lt;/p&gt;

&lt;h2 id=&quot;parsing-the-code&quot;&gt;Parsing The Code&lt;/h2&gt;

&lt;p&gt;If I want to compare two programs, I first need a parse tree for each
program. I need an accurate lexer, a basic parser, and I need to
preserve comments.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://tree-sitter.github.io/tree-sitter/&quot;&gt;tree-sitter&lt;/a&gt; was a great
fit here. You define a grammar in JSON or JS, and it generates a C
library that anyone can use. It’s not 100% accurate (e.g. the C++
parser doesn’t have preprocessor data) but it’s more than good enough.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;list: ($) =&amp;gt; seq(&quot;(&quot;, repeat($._sexp), &quot;)&quot;),
vector: ($) =&amp;gt; seq(&quot;[&quot;, repeat($._sexp), &quot;]&quot;),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here’s an excerpt from my &lt;a href=&quot;https://github.com/Wilfred/tree-sitter-elisp&quot;&gt;Emacs Lisp
grammar&lt;/a&gt;. There’s a ton
of tree-sitter parsers available too. Difftastic now supports 44
different syntaxes, and adding new ones is so straightforward that my
&lt;a href=&quot;https://difftastic.wilfred.me.uk/adding_a_parser.html&quot;&gt;manual includes a worked
example&lt;/a&gt;.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/difftastic_elisp.png&quot; /&gt;
    &lt;figcaption&gt;Using difftastic with Emacs Lisp&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;After parsing, difftastic converts the tree-sitter parse tree to an
s-expression. Everything is a list or an atom. This uniform
representation enables the diffing logic to work on any language that
I can parse.&lt;/p&gt;

&lt;p&gt;For example, given a JavaScript program like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;foo(1, 2)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;tree-sitter parses it to this parse tree:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;expression_statement
  call_expression
    identifier &quot;foo&quot;
    arguments
      (
      number &quot;1&quot;
      ,
      number &quot;2&quot;
      )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;difftastic then converts the tree to this s-expression representation:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;List {
    open_content: &quot;&quot;,
    children: [
        Atom &quot;foo&quot;,
        List {
            open_content: &quot;(&quot;,
            children: [
                Atom &quot;1&quot;,
                Atom &quot;,&quot;,
                Atom &quot;2&quot;,
            ],
            close_content: &quot;)&quot;,
        },
    ],
    close_content: &quot;&quot;,
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;calculating-the-diff&quot;&gt;Calculating The Diff&lt;/h2&gt;

&lt;p&gt;Fun fact: I thought of diffing programs as working out what has
changed. &lt;strong&gt;The goal of diffing is actually to work out what hasn’t
changed!&lt;/strong&gt; The more stuff you can match up, the smaller and more
readable your diff.&lt;/p&gt;

&lt;p&gt;How do I work out what’s changed between two parse trees? There are a
bunch of different design ideas out there, but
&lt;a href=&quot;https://fazzone.github.io/autochrome.html&quot;&gt;autochrome&lt;/a&gt; is the most
effective approach I found, and it includes an incredibly helpful
worked example.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/autochrome.png&quot; /&gt;
    &lt;figcaption&gt;Autochrome output example&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Autochrome and difftastic represent diffing as a &lt;strong&gt;shortest path
problem&lt;/strong&gt; on a directed acyclic graph. A vertex represents a pair of
positions: the position in the left-hand side s-expression (before), and the position in
the right-hand side s-expression (after).&lt;/p&gt;

&lt;p&gt;The goal is to find the shortest route from the start vertex (where
both positions are before the first item in the programs) to the end
vertex (where both positions are after the last item in the program).&lt;/p&gt;

&lt;p&gt;For example, suppose you’re comparing the program &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X A&lt;/code&gt;. The
vertices look like this.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;START
+---------------------+
| Left: A  Right: X A |
|       ^         ^   |
+---------------------+

END
+---------------------+
| Left: A  Right: X A |
|        ^           ^|
+---------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The edges in the graph represent the changes required to transform the
left-hand side program into the right-hand side.&lt;/p&gt;

&lt;p&gt;In this example, there are two possible changes from the start vertex,
so it has two edges. Difftastic could (1) consider the left-hand side to
be novel and increment that position, or (2) it could consider the
right-hand side to be novel and increment that position.&lt;/p&gt;

&lt;p&gt;A novel item on the left-hand side is a &lt;span style=&quot;color: red&quot;&gt;removal&lt;/span&gt;, and a novel item on
the right-hand side is an &lt;span style=&quot;color: green&quot;&gt;addition&lt;/span&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;            START
            +---------------------+
            | Left: A  Right: X A |
            |       ^         ^   |
            +---------------------+
                   /       \
     Novel atom L /         \ Novel atom R
1                v       2   v
+---------------------+  +---------------------+
| Left: A  Right: X A |  | Left: A  Right: X A |
|        ^        ^   |  |       ^           ^ |
+---------------------+  +---------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When both positions are pointing to an identical s-expression, a third
edge is added in the graph. This edge represents matching an
s-expression on both sides.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;            2
            +---------------------+
            | Left: A  Right: X A |
            |       ^           ^ |
            +---------------------+
                   /    |   \
     Novel atom L /     |    \ Novel atom R
                 v      |     v
+---------------------+ | +---------------------+
| Left: A  Right: X A | | | Left: A  Right: X A |
|        ^          ^ | | |       ^            ^|
+---------------------+ | +---------------------+
  |                     |                    |
  | Novel atom R        | Nodes match        | Novel atom L
  |                     |                    |
  |         END         v                    |
  |         +---------------------+          |
  +--------&amp;gt;| Left: A  Right: X A |&amp;lt;---------+
            |        ^           ^|
            +---------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This ‘unchanged node’ edge has a lower cost than the ‘novel node’
edges. The routing problem is then a matter of finding the route with
the most unchanged edges.&lt;/p&gt;

&lt;p&gt;The shortest route with programs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X A&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Novel Right,
Unchanged]&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;finding-the-shortest-route&quot;&gt;Finding The Shortest Route&lt;/h2&gt;

&lt;p&gt;Difftastic and Autochrome both use Dijkstra’s algorithm. Unfortunately
the size of the graph is quadratic: it’s O(L * R), where L is the
number of items in the left-hand s-expression and R is the number of
items in the right-hand s-expression.&lt;/p&gt;

&lt;p&gt;To make performance bearable, difftastic aggressively discards
obviously unchanged s-expressions at the beginning, middle and end of
the file. If you’ve only changed the last function in a file,
difftastic won’t consider the other functions at all.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/difftastic_unchanged.png&quot; /&gt;
    &lt;figcaption&gt;Everything above the line is identical&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;(GNU Diff has a similar feature with &lt;a href=&quot;https://www.gnu.org/software/diffutils/manual/html_node/diff-Performance.html&quot;&gt;its horizon-lines
option&lt;/a&gt;,
although its performance is much better in general.)&lt;/p&gt;

&lt;p&gt;Due to the sheer size of the graph (several million vertices), the
biggest performance bottleneck is vertex construction. I explored
better route finding algorithms (e.g. A*) but I didn’t see much
improvement. My current solution is to construct the graph lazily, so
few vertices are constructed.&lt;/p&gt;

&lt;p&gt;Even with this, difftastic sometimes struggles with performance. I’ve
profiled and optimised everything I can think of (using Rust really
helped here). If the graph is just too big, difftastic falls back to a
conventional line-oriented diff.&lt;/p&gt;

&lt;h2 id=&quot;what-about-nesting&quot;&gt;What About Nesting?&lt;/h2&gt;

&lt;p&gt;When incrementing positions during graph traversal, I needed to be
careful with entering and leaving delimiters.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;; Before
(x y)

;; After
(x) y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The desired result here is 
&lt;code&gt;(x &lt;span style=&quot;background-color: Salmon&quot;&gt;y&lt;/span&gt;)&lt;/code&gt;
and 
&lt;code&gt;(x) &lt;span style=&quot;background-color: PaleGreen&quot;&gt;y&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the s-expression position is on a delimiter, difftastic can
either consider the delimiter novel, or unchanged. This is similar to
the s-expression atom case.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;               START
               +---------------------------+
               | Left: (x y)  Right: (x) y |
               |       ^             ^     |
               +---------------------------+
                     /        |     \
  Novel delimiter L /         |      \ Novel delimiter R
                   v          |       v
+---------------------------+ | +---------------------------+
| Left: (x y)  Right: (x) y | | | Left: (x y)  Right: (x) y |
|        ^            ^     | | |       ^             ^     |
+---------------------------+ | +---------------------------+
                              |
                              | Unchanged delimiters
                              v
               +---------------------------+
               | Left: (x y)  Right: (x) y |
               |        ^             ^    |
               +---------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What happens when the position is at the end of the list? If
difftastic entered the delimiters together (‘unchanged delimiter’), it
must exit them together. This requires both s-expressions positions to
point to the exit delimiter.&lt;/p&gt;

&lt;p&gt;If the delimiters were entered separately (‘novel delimiter’), then
the delimiters can be exited separately too.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;               +---------------------------+
               | Left: (x y)  Right: (x) y |
               |          ^            ^   |
               +---------------------------+
                     /              \
       Novel node L /                \ Exit delimiter R
                   v                  v
+---------------------------+   +---------------------------+
| Left: (x y)  Right: (x) y |   | Left: (x y)  Right: (x) y |
|           ^           ^   |   |          ^              ^ |
+---------------------------+   +---------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The ‘exit delimiter right’ edge is only allowed if the delimiter was
also entered with ‘novel delimiter right’.&lt;/p&gt;

&lt;p&gt;This means that graph vertices are really a tuple of three items:
(left-hand side position, right-hand side position,
list_of_parents_to_exit_together).&lt;/p&gt;

&lt;p&gt;This exponentially increases the size of the graph, O(2&lt;sup&gt;N&lt;/sup&gt;) where N
is the highest list nesting level in either input.&lt;/p&gt;

&lt;p&gt;I solved this by only considering at most two graph vertices for each
position pair. I always find a route this way (there exists a route to
the end vertex from every other vertex) but it is not necessarily the
shortest. In practice this seems to explore enough of the graph that
the results are consistently great.&lt;/p&gt;

&lt;h2 id=&quot;building-the-interface&quot;&gt;Building The Interface&lt;/h2&gt;

&lt;p&gt;Phew! After wrestling with diffing algorithms for some time, I thought
building the UI would be straightforward. I was wrong.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/difftastic_json.png&quot; /&gt;
    &lt;figcaption&gt;There are almost no lines with the same text content here!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Difftastic knows which s-expression nodes are unchanged. It completely
ignores whitespace.&lt;/p&gt;

&lt;p&gt;However, there is no guarantee that there are any lines in common
between the two files. It’s also possible that a line in the first
file might have matches in zero, one or many lines in the second file.&lt;/p&gt;

&lt;p&gt;The display logic iterates through all the matched lines, and tries to
align as many as possible. It uses a two-column display by default, so
you can reformat the entire file and it will still produce a sensible
output.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/difftastic_align.png&quot; /&gt;
    &lt;figcaption&gt;Difftastic has aligned both format!() expressions here.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Working on diff UIs has made me realise how bad the traditional diff
display is.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/git_diff.png&quot; /&gt;
    &lt;figcaption&gt;Conventional git diff&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@@ -392,7 +392,12 @@&lt;/code&gt; means that the diff starts at
line 392. The user is forced to count lines to work out that the change
itself is on line 395!&lt;/p&gt;

&lt;h2 id=&quot;dogfooding&quot;&gt;Dogfooding&lt;/h2&gt;

&lt;p&gt;Eventually I added the ability to use difftastic with git or
mercurial. This was an exceptional way to find bugs and see how well
the design works in practice.&lt;/p&gt;

&lt;p&gt;This exposed a bunch of subtle issues with structural diffing.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;; Before
(foo (bar))

;; After
(foo (novel) (bar))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Diff algorithms are described in the literature as “finding a minimal
edit script”, where the edit script is the additions/removals required to
turn the input file into the output file.&lt;/p&gt;

&lt;p&gt;In this example, &lt;code&gt;(foo (&lt;span style=&quot;background-color:PaleGreen&quot;&gt;novel&lt;/span&gt;)
&lt;span style=&quot;background-color:PaleGreen&quot;&gt;(&lt;/span&gt;bar&lt;span style=&quot;background-color:PaleGreen&quot;&gt;)&lt;/span&gt;)&lt;/code&gt; would be a totally
valid, minimal diff. It’s adding the symbol &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;novel&lt;/code&gt; and adding one
pair of parentheses.&lt;/p&gt;

&lt;p&gt;This isn’t what the user wants though. They’d rather see &lt;code&gt;(foo &lt;span style=&quot;background-color:PaleGreen&quot;&gt;(novel)&lt;/span&gt; (bar))&lt;/code&gt;
even though it’s equally minimal.&lt;/p&gt;

&lt;p&gt;I solved this by adjusting the graph edge cost model to produce nicer
results. I also added a secondary pass on the diff result to check for more
aesthetically pleasing results with the same edge cost.&lt;/p&gt;

&lt;p&gt;(I encountered several more cases that structural diffs struggle with,
see the &lt;a href=&quot;https://difftastic.wilfred.me.uk/tricky_cases.html&quot;&gt;Tricky Cases page in the
manual&lt;/a&gt; for more
details.)&lt;/p&gt;

&lt;h2 id=&quot;future-work&quot;&gt;Future Work&lt;/h2&gt;

&lt;p&gt;Difftastic is fantastic when it works, and I use it daily. It’s still
not perfect though.&lt;/p&gt;

&lt;p&gt;Difftastic has some complicated failure modes. Changing large string
literals is a challenge (syntactically they’re single atoms, but users
&lt;em&gt;sometimes&lt;/em&gt; want a word-level diff).&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/typescript_literal.png&quot; /&gt;
    &lt;figcaption&gt;The string literals have changed, but it&apos;s hard to spot where.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The minimal diff isn’t always helpful either. Sometimes difftastic goes too far.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;/assets/cpp_similar.png&quot; /&gt;
    &lt;figcaption&gt;Sure, there&apos;s a = on both sides, but it&apos;s distracting.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 id=&quot;closing-thoughts&quot;&gt;Closing Thoughts&lt;/h2&gt;

&lt;p&gt;I had no idea what I was getting into when I started working on this.&lt;/p&gt;

&lt;p&gt;I’d been wondering why this type of tool is so rare. Now I know: it’s
extremely challenging to build. Despite its limitations, I’m surprised
at how often it works &lt;em&gt;fantastically&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Difftastic is OSS under a MIT license, so I hope it enables more diff
tools that can understand structure. If you’re feeling brave, you can
even &lt;a href=&quot;https://difftastic.wilfred.me.uk/installation.html&quot;&gt;try it
yourself&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Tue, 06 Sep 2022 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2022/09/06/difftastic-the-fantastic-diff/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2022/09/06/difftastic-the-fantastic-diff/</guid>
      </item>
    
      <item>
        <title>The Siren Song of Little Languages</title>
        <description>&lt;p&gt;Some programming languages languish due to obscurity. They lack
breathless blog posts exclaiming how much nicer they are to use.&lt;/p&gt;

&lt;p&gt;Other languages are too ambitious. They aspire to support so many
features that the original implementers struggle to get a first
version working. For example, the type system in Fortress
&lt;a href=&quot;https://youtu.be/EZD3Scuv02g?t=50m10s&quot;&gt;required constraint solving which took exponential time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sometimes a usable language struggles simply because &lt;strong&gt;it’s too much
fun to write your own&lt;/strong&gt;. Developers end up building their own implementation
rather than actually &lt;em&gt;using&lt;/em&gt; the language.&lt;/p&gt;

&lt;p&gt;The most obvious implementation-focused language
is &lt;a href=&quot;https://en.wikipedia.org/wiki/Brainfuck&quot;&gt;BF&lt;/a&gt;. Despite
having
&lt;a href=&quot;https://esolangs.org/wiki/Brainfuck_implementations&quot;&gt;many implementations&lt;/a&gt;,
BF programmers have to &lt;a href=&quot;http://www.hevanet.com/cristofd/brainfuck/epistle.html&quot;&gt;encourage the implementers to actually try using the language&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Scheme is also susceptible to this. Wikipedia
lists
&lt;a href=&quot;https://en.wikipedia.org/wiki/Category:Scheme_(programming_language)_implementations&quot;&gt;31 different Scheme implementations&lt;/a&gt;,
not to mention the many toy implementations. Writing a Scheme is a
great introduction to interpreters,
especially
&lt;a href=&quot;http://axisofeval.blogspot.co.uk/2011/01/more-fully-featured-modern-lisps.html&quot;&gt;once you get beyond the minimal lisp featureset&lt;/a&gt;.
I’ve certainly written more implementation code than Scheme code.&lt;/p&gt;

&lt;p&gt;The problem seems to be languages with a small, well written
specification. &lt;a href=&quot;http://www.shenlanguage.org/&quot;&gt;Shen&lt;/a&gt; is a
multiparadigm lisp defined in terms of an
&lt;a href=&quot;http://shenlanguage.org/shendoc.htm#Kl&quot;&gt;elegant base language&lt;/a&gt; with
only 46 system functions. This has resulted in a remarkable
&lt;a href=&quot;https://github.com/Shen-Language/wiki/wiki#ports&quot;&gt;15 third-party implementations&lt;/a&gt;,
but
&lt;a href=&quot;http://www.shenlanguage.org/library.htm&quot;&gt;only a small number of libraries implemented in the
language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This phenomenon is not limited to lisps. Forth is also a language
that developers often prefer to implement rather than
use. &lt;a href=&quot;https://github.com/AlexandreAbreu/jonesforth/blob/master/jonesforth.S&quot;&gt;Jones Forth&lt;/a&gt; is
both a Forth tutorial and a discussion of how to build a Forth
compiler. There are even
&lt;a href=&quot;http://yosefk.com/blog/my-history-with-forth-stack-machines.html&quot;&gt;stories of people spending years working on implementations without learning much of the language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Designing a language with a straightforward implementation is not a
bad thing. It’s just a pitfall that language designers need to be
aware of. &lt;a href=&quot;http://www.moserware.com/2009/09/stick-figure-guide-to-advanced.html#act-3-details&quot;&gt;Some crypto systems have this problem
too&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It seems that we need languages to be big enough that new users write
hello world &lt;em&gt;in&lt;/em&gt; the language, not write a tool for &lt;em&gt;others&lt;/em&gt; to write
hello world. In the Lisp family, Clojure and Racket seem to have
reached a size threshold where newcomers are happy downloading the
canonical implementation.&lt;/p&gt;

&lt;p&gt;This doesn’t mean multiple implementations are bad. It’s a great sign
of language health. We just don’t need these until there’s a community of
&lt;em&gt;users&lt;/em&gt; and there is a need for implementations with different
qualities.&lt;/p&gt;

&lt;p&gt;A language can be too small and elegant.&lt;/p&gt;
</description>
        <pubDate>Sun, 24 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2019/03/24/the-siren-song-of-little-languages/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2019/03/24/the-siren-song-of-little-languages/</guid>
      </item>
    
      <item>
        <title>How High Are Your Tests?</title>
        <description>&lt;p&gt;How big can a unit test be? How small can an integration test be?&lt;/p&gt;

&lt;p&gt;It’s easy to argue about whether a test is a ‘true’ unit test or
not. If we test several classes together, is it still a unit test? If
we use a small external API, must we call it an integration test?&lt;/p&gt;

&lt;p&gt;The problem is that testing is a &lt;strong&gt;spectrum&lt;/strong&gt;, but our terminology only
allows discrete levels.&lt;/p&gt;

&lt;h2 id=&quot;putting-a-number-on-it&quot;&gt;Putting A Number On It&lt;/h2&gt;

&lt;p&gt;I propose we treat testing as a &lt;strong&gt;numerical scale&lt;/strong&gt; instead. Let’s
introduce a concept of &lt;strong&gt;test height&lt;/strong&gt;, from 0 to 100.&lt;/p&gt;

&lt;p&gt;At 0, we have low-level, isolated unit tests. They run quickly, they run
in process, and they’re extremely reliable. No setup or teardown is
required.&lt;/p&gt;

&lt;p&gt;At 100, we have high-level tests of full computer systems. We spin up
an elaborate infrastructure of databases, external services, and
exercise real protocols. Many processes run, runtime can be very
variable, and flakiness is a continuing challenge.&lt;/p&gt;

&lt;p&gt;Let’s look at some examples! We’ll look at some code for a wiki
website, and discuss different tests we could write.&lt;/p&gt;

&lt;h2 id=&quot;20-an-isolated-test&quot;&gt;20: An Isolated Test&lt;/h2&gt;

&lt;p&gt;At the lowest level, we have code that only depends on its inputs.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;slugify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# we don&apos;t want /, # or ? in our URL
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;#&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;?&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# replace whitespace with underscores
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;[-&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s]+&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;_&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can easily write a small unit test for a function like this. There
are no dependencies on external resources, or even external
libraries.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_slugify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;slugify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;foo/bar baz?&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;foobar_baz&apos;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This test only requires a single process, and our test assertion is simply
looking at runtime values.&lt;/p&gt;

&lt;h2 id=&quot;40-running-with-a-scratch-database&quot;&gt;40: Running With A Scratch Database&lt;/h2&gt;

&lt;p&gt;Our wiki stores its data in a database. Let’s look at some view code
that ultimately creates database rows.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_valid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;all_users&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;template_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;form&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;render_to_response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;users/create_user.html&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;template_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;RequestContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can test this after creating a new database to write to.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserCreateTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;django&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_create_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;create_user&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                         &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;username&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;someuser&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                          &lt;span class=&quot;s&quot;&gt;&apos;email&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;some@example.com&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;someuser&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We’re now running two processes during our tests, and we need to
ensure we clean up properly after each test. Assertions are now
verifying that data is being written to the database. Our test is
definitely slower than level 10.&lt;/p&gt;

&lt;h2 id=&quot;60-full-protocol-test&quot;&gt;60: Full Protocol Test&lt;/h2&gt;

&lt;p&gt;Web frameworks make testing easy, and allow you to call view functions
directly. If we want to exercise a full web request, we need something
that speaks HTTP. This is usually a headless web browser.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserCreateTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setUp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;webdriver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PhantomJS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_create_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://localhost/&quot;&lt;/span&gt;
                        &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;create_user&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_element_by_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&apos;username&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;someuser&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_element_by_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&apos;email&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;some@example.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_element_by_css_selector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[type=submit]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# We should have a table listing our new user.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;matching_rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_elements_by_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;td&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;someuser&apos;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matching_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tearDown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This test is even slower still: we’re running the full webserver stack
and driving a browser from our tests. We have to start worrying about
asynchronous responses: when do we know that the browser has fully
loaded the page, so we can look for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;td&lt;/code&gt; tags?&lt;/p&gt;

&lt;p&gt;If this test fails, any errors or exceptions are less likely to be
useful. We’ll probably need to open a browser ourselves and step
through the process to see what happened.&lt;/p&gt;

&lt;p&gt;These tests are definitely useful! They’re higher maintenance, but
they exercise more of your code in a more realistic way.&lt;/p&gt;

&lt;h2 id=&quot;80-system-test&quot;&gt;80: System Test&lt;/h2&gt;

&lt;p&gt;Finally, we simulate the whole system. We could spin up VMs, configure
HTTPS certificates, automatically building the infrastructure for an
entire website.&lt;/p&gt;

&lt;p&gt;(But isn’t this just a staging area? In practice, the line between
testing and staging is extremely blurry anyway.)&lt;/p&gt;

&lt;p&gt;This can give a very high degree of confidence, but it’s even worse
than level 60. It’s slower, even more to maintain, and even flakier.&lt;/p&gt;

&lt;p&gt;I like to have a couple of sanity check tests at this level. For
example, spinning up a full server and checking that the homepage
loads is a great way of catching infrastructure issues.&lt;/p&gt;

&lt;h2 id=&quot;why-dont-we-use-a-scale&quot;&gt;Why Don’t We Use A Scale?&lt;/h2&gt;

&lt;p&gt;If we think about tests on a scale, then we acknowledge there are
extremes, and points in-between. This is why I don’t have any examples
with a height of 0 (not even using the standard library?) or 100 (simulating
an entire data centre?).&lt;/p&gt;

&lt;p&gt;Testing a function that only uses the standard library could be 20,
but if our function also calls other helper functions we’ve written,
it might be 25.&lt;/p&gt;

&lt;p&gt;If our program require a database, but we never written to, we might
only give it a height of 35. It requires no teardown and tests can
even safely run in parallel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our tooling forces discrete levels upon us&lt;/strong&gt;. The tests exist on a
scale, but we have discrete mocking features we can choose to use or
not. A Django test must decide whether it inherits from
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;django.test.TestCase&lt;/code&gt;, which does database setup/teardown, or
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unittest.TestCase&lt;/code&gt;, which does not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sometimes test levels just mean performance&lt;/strong&gt;. Higher level tests
are slower, so projects often split tests into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unit/&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integration/&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quick/&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slow/&lt;/code&gt;. These are &lt;em&gt;arbitrary
designations&lt;/em&gt;, chosen to make testing convenient. They’re not
architectural!&lt;/p&gt;

&lt;h2 id=&quot;beyond-discrete-categories&quot;&gt;Beyond Discrete Categories&lt;/h2&gt;

&lt;p&gt;I hope this scale is a useful tool for you to think about how you
test. What’s right for you depends on what problems you’re facing, the
number of services you depend on, and the complexity of your
stack. Good luck.&lt;/p&gt;
</description>
        <pubDate>Mon, 04 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2019/03/04/how-high-are-your-tests/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2019/03/04/how-high-are-your-tests/</guid>
      </item>
    
      <item>
        <title>Helpful: One Year On</title>
        <description>&lt;p&gt;It’s been a year since the first release of
&lt;a href=&quot;/blog/2017/08/30/helpful-adding-contextual-help-to-emacs/&quot;&gt;Helpful&lt;/a&gt;!
It’s gained a ton of new features, and I’d love to share the
highlights with you.&lt;/p&gt;

&lt;h2 id=&quot;tool-integration&quot;&gt;Tool Integration&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_more_debugging.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Emacs has some excellent built-in debugging tools that I wanted to
expose within Helpful. These tools missed the first release, but I’ve now
had the chance to build them.&lt;/p&gt;

&lt;p&gt;From a Helpful buffer, you can now toggle edebug on a function. This
allows you to easily step through code.&lt;/p&gt;

&lt;p&gt;You can also toggle tracing. Tracing is an underrated built-in feature
of Emacs. You can use it to confirm functions have the inputs and
outputs you’re expecting. It’s really useful when exploring unfamiliar
code.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/emacs_trace.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this example, I’ve enabled tracing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;projectile-project-root&lt;/code&gt; to
see when it’s called, and what values it’s returning.&lt;/p&gt;

&lt;h2 id=&quot;summaries&quot;&gt;Summaries&lt;/h2&gt;

&lt;p&gt;After some great user feedback, Helpful buffers now start with a
summary of what you’re looking at.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/projectile_intro.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Users often want a direct link to the source code, so this is included
in the summary. Helpful also mentions if a function is interactive or
autoloaded, just like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;describe-function&lt;/code&gt;. If a user doesn’t know what
that means, those words now link to the relevant part of the Emacs
manual!&lt;/p&gt;

&lt;h2 id=&quot;aliases&quot;&gt;Aliases&lt;/h2&gt;

&lt;p&gt;Helpful tries to show all relevant information for the current
thing. I’ve overhauled aliases with this in mind.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/obsolete_alias.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For example, if you view &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make-hash-table&lt;/code&gt;, you can now see that there
is another alias of this function, but it’s now deprecated.&lt;/p&gt;

&lt;h2 id=&quot;modifying-variables&quot;&gt;Modifying Variables&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_set_variable.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s now possible to set variables directly from Helpful buffers. This
was inspired by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;counsel-set-variable&lt;/code&gt;, which has an excellent similar
feature.&lt;/p&gt;

&lt;p&gt;If a variable is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defcustom&lt;/code&gt;, then Helpful also includes a link to
the relevant part of Customize.&lt;/p&gt;

&lt;h2 id=&quot;even-better-docstrings&quot;&gt;Even Better Docstrings&lt;/h2&gt;

&lt;p&gt;Helpful now handles all Emacs docstring syntax. It handles references
to keybindings, keymaps, and even supports the obscure features like
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;foo-map&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\=&apos;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/fortran_mode.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fortran-mode&lt;/code&gt; is a great example of a docstring that uses a lot of
Emacs docstring features.&lt;/p&gt;

&lt;p&gt;Recent versions of Helpful try even harder to save you
keystrokes. URLs are now automatically converted to links.&lt;/p&gt;

&lt;p&gt;Finally, my favourite new feature is Info manual links. If a docstring
mentions a section of the manual, Helpful converts it to a link.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_info_reference.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Even better, if a symbol is documented in the Emacs manual, Helpful
automatically adds a link at the bottom of the docstring! This hugely
helps discoverability.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_view_in_manual.png&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;installing-it&quot;&gt;Installing It&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Wilfred/helpful&quot;&gt;Helpful&lt;/a&gt; is available on
&lt;a href=&quot;http://melpa.org/#/helpful&quot;&gt;MELPA&lt;/a&gt; and &lt;a href=&quot;https://stable.melpa.org/#/helpful&quot;&gt;MELPA
stable&lt;/a&gt;. It’s become an
indispensable part of my Emacs workflow, and I’m sure it will benefit
you too.&lt;/p&gt;
</description>
        <pubDate>Fri, 22 Jun 2018 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2018/06/22/helpful-one-year-on/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2018/06/22/helpful-one-year-on/</guid>
      </item>
    
      <item>
        <title>The Emacs Guru Guide to Key Bindings</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;Imagine that you hold Control and type your name into Emacs. Can you
describe what will happen?&lt;/p&gt;

  &lt;p&gt;– The ‘Emacs Guru Test’&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Emacs shortcuts (known as ‘key bindings’) can seem ridiculous to
beginners. Some Emacs users even argue you should change them as soon
as you start using Emacs.&lt;/p&gt;

&lt;p&gt;They are wrong. In this post, I’ll describe the logic behind the Emacs
key bindings. Not only will you be closer to passing the guru test,
but you might even find you like some of the defaults!&lt;/p&gt;

&lt;h2 id=&quot;there-are-how-many&quot;&gt;There Are &lt;em&gt;How&lt;/em&gt; Many?&lt;/h2&gt;

&lt;p&gt;Emacs has a &lt;em&gt;ton&lt;/em&gt; of key bindings.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;nv&quot;&gt;ELISP&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;global-map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;143&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Emacs is a modal editor, so most key bindings are
mode-specific. However, my current Emacs instance has well over a
hundred global shortcuts that work everywhere.&lt;/p&gt;

&lt;p&gt;(Keymaps are nested data structures, so this actually undercounts! For
example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-h C-h&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-h f&lt;/code&gt; are not counted separately.)&lt;/p&gt;

&lt;p&gt;Even that is a drop in the bucket compared with how many commands we
could define key bindings for.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;nv&quot;&gt;ELISP&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;mapatoms&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;commandp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;setq&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;1+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;8612&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;How can we possibly organise all these commands?&lt;/p&gt;

&lt;h2 id=&quot;mnemonic-key-bindings&quot;&gt;Mnemonic Key Bindings&lt;/h2&gt;

&lt;p&gt;Basic commands are often given key bindings based on their name. You’ll
encounter all of these important commands in the Emacs tutorial.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Key Binding&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;e&lt;strong&gt;X&lt;/strong&gt;ecute-extended-command&lt;/td&gt;
      &lt;td&gt;M-x&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;N&lt;/strong&gt;ext-line&lt;/td&gt;
      &lt;td&gt;C-n&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;P&lt;/strong&gt;revious-line&lt;/td&gt;
      &lt;td&gt;C-p&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;F&lt;/strong&gt;orward-char&lt;/td&gt;
      &lt;td&gt;C-f&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;B&lt;/strong&gt;ackward-car&lt;/td&gt;
      &lt;td&gt;C-b&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;i&lt;strong&gt;S&lt;/strong&gt;earch-forward&lt;/td&gt;
      &lt;td&gt;C-s&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Mnemonics are a really effective way of memorising things. If you can
remember the name of the command, you can probably remember the
key binding too.&lt;/p&gt;

&lt;h2 id=&quot;organised-key-bindings&quot;&gt;Organised Key Bindings&lt;/h2&gt;

&lt;p&gt;Many Emacs movement commands are laid out in a consistent pattern.&lt;/p&gt;

&lt;p&gt;For example, movement by certain amount:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Key Binding&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;forward-char&lt;/td&gt;
      &lt;td&gt;C-f&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;forward-word&lt;/td&gt;
      &lt;td&gt;M-f&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;forward-sexp&lt;/td&gt;
      &lt;td&gt;C-M-f&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Moving to the end of something:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Key Binding&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;move-end-of-line&lt;/td&gt;
      &lt;td&gt;C-e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;forward-sentence&lt;/td&gt;
      &lt;td&gt;M-e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;end-of-defun&lt;/td&gt;
      &lt;td&gt;C-M-e&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Transposing, which swaps text either side of the cursor:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Key Binding&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;transpose-chars&lt;/td&gt;
      &lt;td&gt;C-t&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;transpose-words&lt;/td&gt;
      &lt;td&gt;M-t&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;transpose-sexps&lt;/td&gt;
      &lt;td&gt;C-M-t&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Killing text:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Key Binding&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;kill-line&lt;/td&gt;
      &lt;td&gt;C-k&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;kill-sentence&lt;/td&gt;
      &lt;td&gt;M-k&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;kill-sexp&lt;/td&gt;
      &lt;td&gt;C-M-k&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Have you spotted the pattern?&lt;/p&gt;

&lt;p&gt;The pattern here is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-whatever&lt;/code&gt; commands are usually
small, dumb text operations. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-whatever&lt;/code&gt; commands are larger, and
usually operate on words.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-M-whatever&lt;/code&gt; commands are slightly magical. These commands
understand the code they’re looking at, and operate on whole
expressions. Emacs uses the term ‘sexp’ (s-expression), but these
commands usually work in any programming language!&lt;/p&gt;

&lt;h2 id=&quot;discovering-key-bindings&quot;&gt;Discovering Key Bindings&lt;/h2&gt;

&lt;p&gt;What happens when you press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-a&lt;/code&gt;? Emacs can tell you. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-h k C-a&lt;/code&gt;
will show you exactly what command is run.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/describe_key.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you use a command without its key binding, Emacs will helpfully
remind you there’s a shortcut available.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/emacs_hint.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can even do this backwards! If Emacs has done something neat or
unexpected, you might wonder what command ran. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-h l&lt;/code&gt; will reveal
what the command was, and exactly which keys triggered it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/view_lossage.png&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;room-for-emacs&quot;&gt;Room For Emacs&lt;/h2&gt;

&lt;p&gt;Why are Emacs key bindings different from conventional shortcuts? Why
doesn’t &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c&lt;/code&gt; copy text to the clipboard, like many other programs?&lt;/p&gt;

&lt;p&gt;Emacs uses mnemonics for its clipboard commands: you ‘kill’ and ‘yank’
text, so the key bindings are are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-k&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-y&lt;/code&gt;. If you really want,
you can use
&lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/CUA-Bindings.html&quot;&gt;cua-mode&lt;/a&gt; so
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x&lt;/code&gt; acts as you expect.&lt;/p&gt;

&lt;p&gt;The problem is that Emacs commands are too versatile, too general to
fit in the usual &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-v&lt;/code&gt;. Emacs has &lt;em&gt;four&lt;/em&gt; clipboard
commands:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt;: remove text and insert it into the kill-ring. This is like
  clipboard cut, but you can do it multiple times and Emacs will
  &lt;em&gt;remember every item in your clipboard&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill-ring-save&lt;/code&gt;: copy the selected text into the kill-ring. This
  is like clipboard copy, but you can also do this multiple times.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yank&lt;/code&gt;: insert text from the kill-ring. This is like clipboard
paste.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yank-pop&lt;/code&gt;: replace the previously yanked text with the next item
  in the kill ring. There is no equivalent in a single-item clipboard!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The generality of Emacs means that it’s hard to find a key binding for
everything. Key bindings tend to be slightly longer as a result:
opening a file is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x C-f&lt;/code&gt;, an additional keystroke over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-o&lt;/code&gt; of
other programs.&lt;/p&gt;

&lt;h2 id=&quot;room-for-you&quot;&gt;Room For You&lt;/h2&gt;

&lt;p&gt;With all these key bindings already defined, what bindings should
you use for your personal favourite commands?&lt;/p&gt;

&lt;p&gt;Much like IP addresses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;192.168.x.x&lt;/code&gt; is reserved for private use,
Emacs has
&lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html&quot;&gt;keys that are reserved&lt;/a&gt; for
user configuration. All the sequences &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c LETTER&lt;/code&gt;, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c a&lt;/code&gt;,
are reserved for your usage, as are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;F5&amp;gt;&lt;/code&gt; through to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;F9&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if you find yourself using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imenu&lt;/code&gt; a lot, you might bind
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c i&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;global-set-key&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kbd&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C-c i&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;#&apos;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;imenu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;you-make-the-rules&quot;&gt;You Make The Rules&lt;/h2&gt;

&lt;p&gt;This doesn’t mean that you should never modify key bindings. Emacsers
create
&lt;a href=&quot;https://github.com/chrisdone/god-mode&quot;&gt;weird&lt;/a&gt; &lt;a href=&quot;http://melpa.milkbox.net/#/key-chord&quot;&gt;and&lt;/a&gt; &lt;a href=&quot;https://github.com/abo-abo/hydra&quot;&gt;wonderful&lt;/a&gt; ways
of mapping keys all the time.&lt;/p&gt;

&lt;p&gt;Emacs will even try to accommodate this. If you open the tutorial
after changing a basic key binding, it will update accordingly!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/emacs_tutorial.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The secret to mastering Emacs is to remember everything is
self-documenting. Learn the help commands to find out which commands
have default key bindings. Consider &lt;em&gt;following&lt;/em&gt; the existing patterns
when you define new key bindings or override existing
ones. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org-mode&lt;/code&gt;, for example, redefines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-M-t&lt;/code&gt; to transpose org
elements.&lt;/p&gt;

&lt;p&gt;Once you understand the patterns, you’ll know when to follow and when
to break them. You’ll also be much closer to passing that guru test!&lt;/p&gt;
</description>
        <pubDate>Sat, 06 Jan 2018 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2018/01/06/the-emacs-guru-guide-to-key-bindings/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2018/01/06/the-emacs-guru-guide-to-key-bindings/</guid>
      </item>
    
      <item>
        <title>These Weeks in Remacs III</title>
        <description>&lt;p&gt;Time for another Remacs update: lots of contributions, a wide range of
features, and even a logo!&lt;/p&gt;

&lt;h2 id=&quot;contributing&quot;&gt;Contributing&lt;/h2&gt;

&lt;p&gt;Since the &lt;a href=&quot;/blog/2017/07/15/these-weeks-in-remacs-ii/&quot;&gt;last update&lt;/a&gt;,
we’ve seen contributions from lots of new people. We’ve added
@brotzeit and @shanavas786, bringing us to seven wonderful people who
can approve your PRs.&lt;/p&gt;

&lt;p&gt;Speaking of PRs, we’ve merged an amazing 64 pull requests since the
last update!&lt;/p&gt;

&lt;p&gt;If you’re looking for a good feature for your first contribution,
@brotzeit has been regularly adding
&lt;a href=&quot;https://github.com/Wilfred/remacs/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22&quot;&gt;new suggestions under the ‘good first issue’ label&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;features&quot;&gt;Features&lt;/h2&gt;

&lt;p&gt;Many Emacs features have now been ported to Rust, with new Rust APIs
for accessing elisp datastructures.&lt;/p&gt;

&lt;p&gt;Here’s an overview of the features that have landed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arithmetic&lt;/strong&gt;:
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/311/files&quot;&gt;arithmetic&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/234/files&quot;&gt;floating point&lt;/a&gt;, 
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/235/files&quot;&gt;random number generation&lt;/a&gt; (using
a Rust RNG!), and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/226/files&quot;&gt;comparisons&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symbols&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/235/files&quot;&gt;symbol properties&lt;/a&gt;, 
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/255&quot;&gt;interning&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/267&quot;&gt;obarrays&lt;/a&gt;
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/308/files&quot;&gt;unbinding&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/334/files&quot;&gt;keywords&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/357/files&quot;&gt;indirect symbols&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checksums&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/260/files&quot;&gt;MD5sum&lt;/a&gt;
(using a Rust MD5 crate!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Windows&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/258/files&quot;&gt;liveness check&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/240/files&quot;&gt;type check&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/298/files&quot;&gt;overlays and minibuffer&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/319/files&quot;&gt;minibuffer check&lt;/a&gt;
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/317/files&quot;&gt;positions&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/351/files&quot;&gt;margins&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Processes&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/256/files&quot;&gt;accessing&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/265/files&quot;&gt;type check&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/325/files&quot;&gt;data structures&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/341&quot;&gt;names&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Buffers&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/253/files&quot;&gt;for the current thread&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/242/files&quot;&gt;accessing&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/279/files&quot;&gt;file names&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/281/files&quot;&gt;size&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/288/files&quot;&gt;modification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/291/files&quot;&gt;bobp&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/293/files&quot;&gt;bolp, eolp&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/297/files&quot;&gt;markers&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/302/files&quot;&gt;point-min, point-max&lt;/a&gt;
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/321/files&quot;&gt;forward-point&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/352/files&quot;&gt;goto-char&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash tables&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/251/files&quot;&gt;copying&lt;/a&gt; and &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/349&quot;&gt;accessing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Characters&lt;/strong&gt;:
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/236/files&quot;&gt;multibyte conversions&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/277/files&quot;&gt;character tables&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/282/files&quot;&gt;category tables&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fonts&lt;/strong&gt;: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/248&quot;&gt;type checks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Miscellaneous&lt;/strong&gt;:
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/252/files&quot;&gt;prefix arguments&lt;/a&gt; and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/329/files&quot;&gt;identity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We’re also periodically pulling GNU Emacs features into Remacs, so all
the features available GNU Emacs trunk are included in Remacs.&lt;/p&gt;

&lt;h2 id=&quot;idiomatic-rust-in-remacs&quot;&gt;Idiomatic Rust in Remacs&lt;/h2&gt;

&lt;p&gt;Remacs has gradually developed a set of conventions for elisp data
types. For each type Foo, we define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LispObject::as_foo&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LispObject::as_foo_or_error&lt;/code&gt; and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FooRef&lt;/code&gt; when you know your elisp
datatype is actually a Foo.&lt;/p&gt;

&lt;p&gt;For example, here’s how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlay-start&lt;/code&gt; was implemented in C:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;n&quot;&gt;DEFUN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;overlay-start&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Foverlay_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Soverlay_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* Return the position at which OVERLAY starts.  */&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Lisp_Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;overlay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;CHECK_OVERLAY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;overlay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fmarker_position&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVERLAY_START&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;overlay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The C codebase makes heavy use of macros for checking types
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHECK_OVERLAY&lt;/code&gt;) and for accessing struct attributes
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OVERLAY_START&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Here’s the Rust equivalent:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;cd&quot;&gt;/// Return the position at which OVERLAY starts.&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;#[lisp_fn]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;overlay_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;overlay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LispObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LispObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;overlay&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.as_overlay_or_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;marker_position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We use procedural macros to simplify defining an elisp primitive
function, and type checking is much more explicit.&lt;/p&gt;

&lt;p&gt;(This example is
from &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/298/files&quot;&gt;PR #298&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Other exciting Rusty features
include
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/266/files&quot;&gt;variadic macros to replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call2&lt;/code&gt; in C with just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call!&lt;/code&gt; in Rust&lt;/a&gt;,
and
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/304&quot;&gt;the ability to mock extern C functions so we can write unit tests&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;hash-maps&quot;&gt;Hash Maps&lt;/h2&gt;

&lt;p&gt;We’re not always able to leverage the Rust libraries
available. @DavidDeSimone showed some amazing Rust-fu &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/286&quot;&gt;exploring
using Rust’s FnvHashMap inside Remacs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sadly, we weren’t able to use the Rust hash map implementation. The C
layer assumes that it can mutate hash table keys in place, and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unexec&lt;/code&gt; does not play nicely with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mmap&lt;/code&gt;. See the PR for the full details.&lt;/p&gt;

&lt;h2 id=&quot;logo&quot;&gt;Logo&lt;/h2&gt;

&lt;p&gt;Finally, we’re discussing a logo for Remacs. We’ve had some great submissions:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/remacs1.png&quot; /&gt;
&lt;img src=&quot;/assets/remacs2.png&quot; /&gt;
&lt;img src=&quot;/assets/remacs3.png&quot; /&gt;
&lt;img src=&quot;/assets/remacs4.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can join the logo discussion at &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/360&quot;&gt;PR #360&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As always, if you fancy writing some Rust in support of the world’s
lispiest text editor, you can &lt;a href=&quot;https://github.com/Wilfred/remacs&quot;&gt;join us on GitHub&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Mon, 16 Oct 2017 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2017/10/16/these-weeks-in-remacs-iii/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2017/10/16/these-weeks-in-remacs-iii/</guid>
      </item>
    
      <item>
        <title>Helpful: Adding Contextual Help to Emacs</title>
        <description>&lt;p&gt;I’ve just released &lt;a href=&quot;https://github.com/Wilfred/helpful&quot;&gt;Helpful&lt;/a&gt;, a
new way of getting help in Emacs!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*Help*&lt;/code&gt; built-in to Emacs is already pretty good. Helpful
goes a step further and includes lots of contextual info. Let’s take a
look.&lt;/p&gt;

&lt;p&gt;Have you ever wondered which major modes have a keybinding for a
function? Helpful reports keybindings in all keymaps!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_bindings.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When you’re hacking on some new code, you might end up with old
function aliases after renaming a function. Helpful provides
discoverable debug buttons, so you don’t need to remember
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmakunbound&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_tools.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Helpful also has strong opinions on viewing docstrings. Summaries
are given focus, and text is fontified. We solve the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;text-quoting-style&lt;/code&gt; debate by removing superfluous puncuation
entirely.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_docstring.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Helpful will even show all the references to the symbol you’re
looking at,
using &lt;a href=&quot;https://github.com/Wilfred/elisp-refs&quot;&gt;elisp-refs&lt;/a&gt;. This is
great for understanding how and where a function is used.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_refs.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally, Helpful will rifle through your Emacs instance to find
source code to functions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;If you’ve defined a function interactively, Helpful will
use edebug properties to find the source code.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If Emacs can only find the raw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closure&lt;/code&gt;, helpful will convert it
back to an equivalent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defun&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If Emacs can only find the byte-compiled files, helpful will just
pretty-print that.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_interactive_defun.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/helpful_closure.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I’ve just released v0.1, so there will be bugs. Please give it a try,
and let me know what you think, or how we can make it even more, well,
helpful!&lt;/p&gt;
</description>
        <pubDate>Wed, 30 Aug 2017 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2017/08/30/helpful-adding-contextual-help-to-emacs/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2017/08/30/helpful-adding-contextual-help-to-emacs/</guid>
      </item>
    
      <item>
        <title>Suggest.el: Synthesising Constants</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/Wilfred/suggest.el&quot;&gt;Suggest.el&lt;/a&gt; v0.4 is now out,
and it offers some really interesting new ways of making suggestions.&lt;/p&gt;

&lt;h2 id=&quot;supplying-constants&quot;&gt;Supplying Constants&lt;/h2&gt;

&lt;p&gt;Suppose the user gives us the input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;(a b c d)&lt;/code&gt; and desired output
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;a&lt;/code&gt;. We would already suggest &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt;, but that only gets the first
element of the list. They may have wanted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elt&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nth&lt;/code&gt;, which get
elements at a specific position.&lt;/p&gt;

&lt;p&gt;We now try adding constants to the user’s inputs, specifically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nil&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;. This makes suggest.el much more
effective.&lt;/p&gt;

&lt;p&gt;Here’s the example we mentioned:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;&apos;a&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;elt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nth&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can now suggest grouping items in a list pairwise:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;-partition&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Converting a vector to a list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;string-to-list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;append&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;-rotate&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;-concat&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Truncating lists:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;-drop&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;-slice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Choosing good values for constants is difficult, but the current set
seems to be a good tradeoff between performance, the likelihood of
finding a result, and the number of useful results.&lt;/p&gt;

&lt;h2 id=&quot;ranking-suggestions&quot;&gt;Ranking Suggestions&lt;/h2&gt;

&lt;p&gt;Now we have more possibilities, ordering our suggestions is more
complex. The first prototype didn’t always get the ordering correct:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;1+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The user is probably looking for the increment function, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1+&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+ 0
1)&lt;/code&gt; feels like stating the obvious.&lt;/p&gt;

&lt;p&gt;Suggest.el prefers function calls that don’t require extra arguments,
giving us a better order:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Inputs (one per line):&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Desired output:&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Suggestions:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;1+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &amp;lt;- new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;smarter-search&quot;&gt;Smarter Search&lt;/h2&gt;

&lt;p&gt;If suggest.el tries a function and it returns, we save it. If it isn’t
the value we’re looking for, it’s saved to a list of ‘intermediate’
values. This is important for finding nested function calls.&lt;/p&gt;

&lt;p&gt;Some intermediate values are very common, especially when exploring
arithmetic. There are many ways you can convert an integer to the same
float value, for example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sqrt&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fround&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ffloor&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fceiling&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ftruncate&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;;=&amp;gt; 0.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Suggest.el previously considered every single way of generating the
same value. v0.4 only tries each unique value 3 times. This allows us
to explore more unique possibilities, increasing the likelihood of
finding a result before giving up.&lt;/p&gt;

&lt;h2 id=&quot;literature-review&quot;&gt;Literature Review&lt;/h2&gt;

&lt;p&gt;Finally, I’ve spent some time researching similar tools, and
documented them in
&lt;a href=&quot;https://github.com/Wilfred/suggest.el#related-projects&quot;&gt;the related projects section&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Not only has it been interesting to see other approaches, suggest.el
has benefited from the comparisons. The Smalltalk Finder, for example,
also explores bitwise operations, so suggest.el does too!&lt;/p&gt;

&lt;p&gt;If there are simple code snippets that you think suggest.el should
find, please
&lt;a href=&quot;https://github.com/Wilfred/suggest.el/issues/new&quot;&gt;file a bug&lt;/a&gt;. I’m
routinely surprised by the results it finds, but I’m sure it could be
smarter still.&lt;/p&gt;
</description>
        <pubDate>Sun, 06 Aug 2017 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2017/08/06/suggest-el-synthesising-constants/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2017/08/06/suggest-el-synthesising-constants/</guid>
      </item>
    
      <item>
        <title>Optimising Dash.el</title>
        <description>&lt;p&gt;Dash.el is a lovely library, and one of the most popular on MELPA. If
we can squeeze every last drop of performance out of it, everyone
benefits.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the black art of making elisp faster.&lt;/p&gt;

&lt;h2 id=&quot;measure-first&quot;&gt;Measure First!&lt;/h2&gt;

&lt;p&gt;Chris Wellons has a
&lt;a href=&quot;http://nullprogram.com/blog/2017/01/30/&quot;&gt;great optimisation blog post&lt;/a&gt; that
discusses the performance overhead of creating lambdas with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapcar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we look at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--map&lt;/code&gt;, it does indeed create anonymous functions:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defmacro&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Anaphoric form of `-map&apos;.&quot;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mapcar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Creating anonymous functions instantiates a closure, which isn’t
free. Let’s write an iterative equivalent:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defmacro&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;--map-loop&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;declare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;debug&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;result-sym&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-symbol&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;result&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;result-sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dolist&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;result-sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nreverse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;result-sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;List Length&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;mapcar (seconds)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;dolist (seconds)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.000010&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.000028&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.0027&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.0079&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;100,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.74&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1.24&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;(&lt;a href=&quot;https://gist.github.com/Wilfred/d51db0a1433ec4abdbca58a0dec039a5&quot;&gt;Full benchmark code here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Surprisingly, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapcar&lt;/code&gt; is consistently faster in this particular
benchmark! Other Emacsers have
&lt;a href=&quot;https://gist.github.com/Wilfred/d51db0a1433ec4abdbca58a0dec039a5#gistcomment-2019226&quot;&gt;observed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dolist&lt;/code&gt; outperforming &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapcar&lt;/code&gt;&lt;/a&gt; for
short lists.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapcar&lt;/code&gt; is primitive, and primitives tend to be fast. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dolist&lt;/code&gt;
clearly isn’t a speedup in all situations. Let’s try something else.&lt;/p&gt;

&lt;h2 id=&quot;matching-primitive-performance&quot;&gt;Matching Primitive Performance&lt;/h2&gt;

&lt;p&gt;Some dash.el functions are equivalent to primitive functions. For
example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-first-item&lt;/code&gt; is equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-drop&lt;/code&gt; is equivalent
to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nthcdr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-first-item&lt;/code&gt; like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-first-item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, this adds the overhead of an extra function call compared
with calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; directly. Instead, dash.el does this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;defalias&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;-first-item&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;car&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let’s do a small benchmark, to ensure that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defalias&lt;/code&gt; giving us the
performance we want:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Approach&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;time (seconds)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;wrapper function&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.1399&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;alias&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.0055&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;use car directly&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.0050&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;(&lt;a href=&quot;https://gist.github.com/Wilfred/3ee025116cff82169435262f44bffb0c&quot;&gt;Full benchmark code here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;For shame! Our alias still isn’t as fast as using the primitive. Let’s
compare the disassembly using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-x disassemble&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;defalias&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;car-alias&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;car&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;use-car-alias&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;car-alias&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; byte code for use-car-alias:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;;   args: (x)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 0       constant  car-alias&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 1       varref    x&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 2       call      1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 3       return    &lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;use-car-directly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; byte code for use-car-directly:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;;   args: (x)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 0       varref    x&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 1       car       &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 2       return    &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Intriguingly, these are not the same. There’s a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; bytecode that’s
being used with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use-car-directly&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With a little
&lt;a href=&quot;https://emacs.stackexchange.com/q/30064/304&quot;&gt;help from the Emacs Stack Exchange&lt;/a&gt;,
we can see that byte-opt.el looks for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;byte-opcode&lt;/code&gt; properties on
functions. If a function symbol has this property, the byte-compiler
will replace the function with custom bytecode.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; Ensure that calls to `-first-item&apos; are compiled to &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; a single opcode, just like `car&apos;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;put&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;-first-item&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;byte-opcode&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;byte-car&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;put&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;-first-item&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;byte-compile&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&apos;byte-compile-one-arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This makes performance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-first-item&lt;/code&gt; indistinguishable from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt;!
We do lose the ability to advise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-first-item&lt;/code&gt;, but that’s not
possible with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; either.&lt;/p&gt;

&lt;h2 id=&quot;leveraging-the-byte-compiler&quot;&gt;Leveraging the Byte-Compiler&lt;/h2&gt;

&lt;p&gt;What about functions that aren’t just aliases? Can the byte-compiler
help us here?&lt;/p&gt;

&lt;p&gt;It turns out that the byte-compiler can actually calculate values at
compile time!&lt;/p&gt;

&lt;p&gt;Suppose we define a pure function that drops the first two items of a
list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;drop-2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;use-drop-2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%S&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;drop-2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; byte code for use-drop-2:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; args: nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 0       constant  message&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 1       constant  &quot;%S&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 2       constant  drop-2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 3       constant  (1 2 3 4)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 4       call      1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 5       call      2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 6       return    &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If we annotate our function as pure, the byte-compiler helpfully runs
it at compile time:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;drop-2-pure&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;declare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;use-drop-2-pure&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%S&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;drop-2-pure&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; byte code for use-drop-2-pure:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;;   args: nil&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 0       constant  message&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 1       constant  &quot;%S&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 2       constant  (3 4)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 3       call      2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 4       return    &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This works because we’re calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drop-2-pure&lt;/code&gt; on a literal, and we
know the value of literals at compile time.&lt;/p&gt;

&lt;p&gt;We can even annotate our functions as having no side effects. In this
situation, the byte-compiler removes the call entirely:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-common-lisp&quot; data-lang=&quot;common-lisp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;;; eval-and-compile to work around Emacs bug #24863.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;eval-and-compile&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;drop-2-pure&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;declare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;side-effect-free&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pointless-call-to-drop-2-pure&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;drop-2-pure&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; byte code for pointless-call-to-drop-2-pure:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;;   doc:   ...&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;;   args: (arg1)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 0       constant  &quot;foo&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; 1       return    &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The byte-compiler helpfully reports a warning here too:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;value returned from (drop-2-pure x) is unused
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;open-source-ftw&quot;&gt;Open Source FTW&lt;/h2&gt;

&lt;p&gt;The latest version of dash.el includes all these improvements, so you
can simply upgrade to take advantage. If you find yourself needing to
squeeze every last drop of performance from your elisp, you can follow
what we’ve done here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;benchmark your code (with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;benchmark-run&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;profiler-start&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;disassemble your functions (with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;disassemble&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;ask some friendly Emacsers
(e.g. the &lt;a href=&quot;https://emacs.stackexchange.com/&quot;&gt;Emacs Stack Exchange&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good luck! May your editing experience never be laggy!&lt;/p&gt;
</description>
        <pubDate>Sat, 29 Jul 2017 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2017/07/29/optimising-dash-el/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2017/07/29/optimising-dash-el/</guid>
      </item>
    
      <item>
        <title>These Weeks in Remacs II</title>
        <description>&lt;p&gt;It’s been six months since the last Remacs update, and many new
features have landed!&lt;/p&gt;

&lt;h2 id=&quot;community&quot;&gt;Community&lt;/h2&gt;

&lt;p&gt;We now have
a &lt;a href=&quot;https://gitter.im/remacs-discuss/Lobby&quot;&gt;Gitter chat room&lt;/a&gt;! Do drop
by if you have any questions or wish to discuss Remacs. There’s a
low traffic &lt;a href=&quot;https://www.reddit.com/r/remacs/&quot;&gt;Remacs Subreddit&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;We’ve added @jeandudey and @birkenfeld to the GitHub collaborators,
bringing us to five fine people who can approve your pull requests.&lt;/p&gt;

&lt;h2 id=&quot;elisp-features&quot;&gt;Elisp Features&lt;/h2&gt;

&lt;p&gt;We’re still tracking upstream GNU Emacs master, so new features there
are landing in Remacs
(&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/158&quot;&gt;1&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/227&quot;&gt;2&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We’ve added a lot new elisp primitive functions:&lt;/p&gt;

&lt;p&gt;Strings: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/170&quot;&gt;characterp&lt;/a&gt;,
multibyte conversions
(&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/210&quot;&gt;1&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/211&quot;&gt;2&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/218&quot;&gt;3&lt;/a&gt;), and &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/217&quot;&gt;comparisons&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Vectors:
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/202/commits/24248b43295f47f32fe6ba0c74cc60c9c18747f9&quot;&gt;type definitions&lt;/a&gt;,
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/213/&quot;&gt;functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Buffers:
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/202/commits/c7f81453a47ae8ebfd9d7e45bb8909b73e87d886&quot;&gt;type definitions&lt;/a&gt;, &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/215&quot;&gt;functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Symbols: &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/224&quot;&gt;various functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A much requested feature, adding Rust support to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find-function&lt;/code&gt;, &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/203&quot;&gt;has
been added&lt;/a&gt;. This was an
unusual PR as it includes some elisp changes in Remacs.&lt;/p&gt;

&lt;p&gt;We now have
&lt;a href=&quot;https://github.com/Wilfred/remacs/blob/master/REMACS_COMPATIBILITY.md&quot;&gt;documentation on our compatibility with GNU Emacs&lt;/a&gt;. This
covers all known implementation differences, platform support
differences, and describes how to detect Remacs in elisp code.&lt;/p&gt;

&lt;h2 id=&quot;cleanup&quot;&gt;Cleanup&lt;/h2&gt;

&lt;p&gt;Platforms: We’ve
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/140&quot;&gt;dropped MS-DOS support&lt;/a&gt;. The
Remacs build has been fixed on 32-bit Linux and 32-bit macOS.&lt;/p&gt;

&lt;p&gt;The codebase has been split out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;remacs-lib (Rust equivalents of gnulib)&lt;/li&gt;
  &lt;li&gt;remacs-sys (type definitions of Emacs types and C functions)&lt;/li&gt;
  &lt;li&gt;remacs-macros (procedural macros supporting elisp primitive
functions in Rust)&lt;/li&gt;
  &lt;li&gt;src (Rust implementation code of elisp)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Signal name mapping is &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/165&quot;&gt;pure Rust code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We now run &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/151&quot;&gt;rustfmt on every PR&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you fancy building Remacs without installing a dev toolchain
(compilers, C libraries etc), there’s now
a &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/205&quot;&gt;docker-compose.yml&lt;/a&gt; to
make your life easy.&lt;/p&gt;

&lt;h2 id=&quot;macros&quot;&gt;Macros&lt;/h2&gt;

&lt;p&gt;It wouldn’t be a proper lisp project without some macro magic.&lt;/p&gt;

&lt;p&gt;After several PRs and discussions, Remacs now includes
a &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/192&quot;&gt;procedural macro&lt;/a&gt; to
simplify defining elisp functions in Rust.&lt;/p&gt;

&lt;p&gt;For example, here’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vectorp&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;cd&quot;&gt;/// Return t if OBJECT is a vector.&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;#[lisp_fn]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;vectorp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LispObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LispObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;LispObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;from_bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;leveraging-rust&quot;&gt;Leveraging Rust&lt;/h2&gt;

&lt;p&gt;Remacs now
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/162/files&quot;&gt;uses Rust crates for SHA-1 and SHA-2&lt;/a&gt;,
and for
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/136/files&quot;&gt;base64 encoding&lt;/a&gt;. We’ve
even
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/195&quot;&gt;replaced some C bit-counting functions&lt;/a&gt;
with functions from the Rust stdlib.&lt;/p&gt;

&lt;p&gt;Rust has also enabled us to
&lt;a href=&quot;https://github.com/Wilfred/remacs/pull/182&quot;&gt;mark Emacs functions with the ! type&lt;/a&gt;,
a neat Rust feature that marks functions as not returning.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/rust-lang/rfcs/pull/1758&quot;&gt;#[repr(transparent)] Rust RFC&lt;/a&gt;
has been approved, so we’re looking forward to using that in Remacs. In the 
meantime, Remacs has a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LispObject&lt;/code&gt; type for use in Rust, and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLisp_Object&lt;/code&gt; 
type for FFI compatibility.&lt;/p&gt;

&lt;p&gt;Remacs also takes advantage of the
&lt;a href=&quot;https://github.com/rust-lang/rfcs/pull/1398&quot;&gt;user-defined allocators RFC&lt;/a&gt;,
which has been approved too. We’re now &lt;a href=&quot;https://github.com/Wilfred/remacs/pull/221&quot;&gt;up-to-date with the new API&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;phew&quot;&gt;Phew!&lt;/h2&gt;

&lt;p&gt;There’s still lots to do on Remacs: many small elisp functions to port, or
larger projects to sink your teeth into. We also welcome incomplete
pull requests: many of the PRs shown here have been built on top of
initial implementations written by other contributors.&lt;/p&gt;

&lt;p&gt;Join the fun at: &lt;a href=&quot;https://github.com/Wilfred/remacs&quot;&gt;https://github.com/Wilfred/remacs&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 15 Jul 2017 00:00:00 +0000</pubDate>
        <link>https://www.wilfred.me.uk/blog/2017/07/15/these-weeks-in-remacs-ii/</link>
        <guid isPermaLink="true">https://www.wilfred.me.uk/blog/2017/07/15/these-weeks-in-remacs-ii/</guid>
      </item>
    
  </channel>
</rss>
