<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Using results from one code block in another org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2019/02/12/Using-results-from-one-code-block-in-another-org-mode</link>
      <pubDate>Tue, 12 Feb 2019 09:20:58 EST</pubDate>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[elisp]]></category>
      <guid isPermaLink="false">gzZgY5oqeB9thgLDRCfcJu-iwyw=</guid>
      <description>Using results from one code block in another org-mode</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#org679863a"&gt;1. :var&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org1cd48f8"&gt;2. :cache&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org744145d"&gt;3. :wrap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org603dd14"&gt;4. :file&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org61d9dad"&gt;5. "remote" data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#orgf9c3e0a"&gt;6. Manually saving data in files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org578795b"&gt;7. An appendix for data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#org828ee8b"&gt;8. Caveats&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
One really great feature in org-mode is you have many options to pass data between code-blocks. In this post we look at some of these options using emacs-lisp as the language. This runs in a &lt;i&gt;session&lt;/i&gt; where you can keep variables in memory between blocks, and use them in subsequent blocks.
&lt;/p&gt;

&lt;p&gt;
Here we set a variable to a value.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; some-variable 42)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
42
&lt;/pre&gt;


&lt;p&gt;
Then later in another block we can use that variable:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(+ some-variable 1)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
43
&lt;/pre&gt;


&lt;p&gt;
While you are in the session, &lt;code&gt;some-variable&lt;/code&gt; can be used. If you want some mind-bending trouble, the emacs-lisp session is global, and you can access &lt;code&gt;some-variable&lt;/code&gt; even in another buffer! Don't do that. When you close emacs this variable will disappear, and all that is left are the results from above.
&lt;/p&gt;

&lt;p&gt;
There is another way to pass information from one block to another using named src blocks and variables in the block header. This allows you to pass data between blocks by name, and you will see later you can even access the results by name from other files.
&lt;/p&gt;

&lt;div id="outline-container-org679863a" class="outline-2"&gt;
&lt;h2 id="org679863a"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; :var&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
First, we give our src block a name like this:
&lt;/p&gt;

&lt;pre class="example"&gt;
#+name: block-1
#+BEGIN_SRC emacs-lisp
(current-time-string)
#+END_SRC
&lt;/pre&gt;

&lt;p&gt;
When we run this, the results will have a name too.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="org0eee06d"&gt;(current-time-string)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Tue Feb 12 08:19:23 2019
&lt;/pre&gt;


&lt;p&gt;
Now, we can use the named result as &lt;i&gt;input&lt;/i&gt; to a new block using the :var header.
&lt;/p&gt;

&lt;pre class="example"&gt;
#+BEGIN_SRC emacs-lisp :var input=block-1
(format "We got %S in block-1" input)
#+END_SRC
&lt;/pre&gt;

&lt;p&gt;
When we run this block, emacs will run block-1 and put the output in to the variable &lt;code&gt;input&lt;/code&gt; which we use inside the code block.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(format &lt;span style="color: #008000;"&gt;"We got %S in block-1"&lt;/span&gt; input)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got "Tue Feb 12 08:20:44 2019" in block-1
&lt;/pre&gt;


&lt;p&gt;
Some things to note:
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;Every time you run this, block-1 gets rerun.&lt;/li&gt;
&lt;li&gt;The results in this block are not the same as in block-1&lt;/li&gt;
&lt;li&gt;The results in block-1 are not changed when you run the second block.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
You may not want to rerun block-1 each time; maybe it is an expensive calculation, or maybe it should not be changed. You can prevent this behavior by using the :cache header.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org1cd48f8" class="outline-2"&gt;
&lt;h2 id="org1cd48f8"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; :cache&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
If you specify &lt;code&gt;:cache yes&lt;/code&gt; then org-mode &lt;i&gt;should&lt;/i&gt; store a hash of the code block with the results, and if the code block hasn't changed then it should not run again.
&lt;/p&gt;

&lt;pre class="example"&gt;
#+name: block-2
#+BEGIN_SRC emacs-lisp :cache yes
(current-time-string)
#+END_SRC
&lt;/pre&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="orgb1aedb8"&gt;(current-time-string)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Tue Feb 12 08:06:22 2019
&lt;/pre&gt;


&lt;p&gt;
Now, we use block-2 as input to a block, we see the output is the same as the output from block-2.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(format &lt;span style="color: #008000;"&gt;"We got %S in block-2"&lt;/span&gt; input)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got "Tue Feb 12 08:06:22 2019" in block-2
&lt;/pre&gt;


&lt;p&gt;
Ok, but what if my results are too large to put in the buffer, or too complex for text? You still have some options.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org744145d" class="outline-2"&gt;
&lt;h2 id="org744145d"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; :wrap&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;p&gt;
Suppose we generate some json in one block, and we want to use it in another block. We still want to see the json in the buffer as an intermediate result. We can wrap the output in a json block like this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="orgd0725ee"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;json&lt;/span&gt;)
(json-encode `((&lt;span style="color: #008000;"&gt;"date"&lt;/span&gt; . ,(current-time-string))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="json"&gt;
&lt;p&gt;
{"date":"Tue Feb 12 08:30:20 2019"}
&lt;/p&gt;

&lt;/div&gt;

&lt;p&gt;
Then, we can simply input that output into a new block.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(format &lt;span style="color: #008000;"&gt;"We got %S in json"&lt;/span&gt; input)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got "{\"date\":\"Tue Feb 12 08:30:20 2019\"}
" in json
&lt;/pre&gt;


&lt;p&gt;
This admittedly still pretty simple, text-based data. It is probably not a good idea to do this with binary data.
&lt;/p&gt;

&lt;p&gt;
Note you can refer to this result even in another org-file:
&lt;/p&gt;

&lt;pre class="example"&gt;
#+BEGIN_SRC emacs-lisp :var input=./2019-02-12.org:json
input
#+END_SRC

#+RESULTS:
: {"date":"Tue Feb 12 08:30:20 2019"}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;div id="outline-container-org603dd14" class="outline-2"&gt;
&lt;h2 id="org603dd14"&gt;&lt;span class="section-number-2"&gt;4&lt;/span&gt; :file&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;
&lt;p&gt;
It may be that your data is too large to conveniently put into your org-file, or maybe it is binary data. No problem, just put it into an external file using the :file header. It looks like this:
&lt;/p&gt;

&lt;pre class="example"&gt;
#+name: block-3
#+BEGIN_SRC emacs-lisp :cache yes :file block-3
(require 'json)
(json-encode `(("date" . ,(current-time-string))))
#+END_SRC

#+RESULTS[a14d376653bd8c40a0961ca95f21d8837dddec66]: block-3
[[file:block-3]]
&lt;/pre&gt;


&lt;p&gt;
Note that you have to provide a file name for this. Sometimes that is nice if you want a human recognizable file to send to someone, but it would also be nice if there was an automatic naming scheme, e.g. based on an sha-1 hash of the src block.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="org5f97d46"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;json&lt;/span&gt;)
(json-encode `((&lt;span style="color: #008000;"&gt;"date"&lt;/span&gt; . ,(current-time-string))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;a href="/media/block-3"&gt;block-3&lt;/a&gt; 
&lt;/p&gt;

&lt;p&gt;
Now you can use other tools to check out the file. Here we can still use simple shell tools.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-sh"&gt;cat block-3
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
The output of block-3 is a file name:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;input
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
/Users/jkitchin/Box Sync/kitchingroup/jkitchin/journal/2019/02/12/block-3
&lt;/pre&gt;


&lt;p&gt;
So you can use it in a new block to read the data in, and then do something new with it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
  (insert-file-contents input)
  (format &lt;span style="color: #008000;"&gt;"We got %S in block-3"&lt;/span&gt; (json-read-from-string (buffer-string))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got ((date . "Tue Feb 12 08:46:55 2019")) in block-3
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org61d9dad" class="outline-2"&gt;
&lt;h2 id="org61d9dad"&gt;&lt;span class="section-number-2"&gt;5&lt;/span&gt; "remote" data&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;
&lt;p&gt;
The blocks do not have to be in order. If you want, you can put your blocks in an &lt;a href="#org578795b"&gt;appendix&lt;/a&gt;, and then just have analysis blocks here that use them. That way, you can have short blocks here that are more readable, but longer, more complex blocks elsewhere that do not clutter your document.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
  (insert-file-contents input)
  (format &lt;span style="color: #008000;"&gt;"We got %S in the appendix data"&lt;/span&gt; (json-read-from-string (buffer-string))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got "{\"date\":\"Tue Feb 12 09:11:12 2019\"}" in the appendix data
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;div id="outline-container-orgf9c3e0a" class="outline-2"&gt;
&lt;h2 id="orgf9c3e0a"&gt;&lt;span class="section-number-2"&gt;6&lt;/span&gt; Manually saving data in files&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-6"&gt;
&lt;p&gt;
Note you can also manually save data in a file, for example:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="org99c8008"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;json&lt;/span&gt;)
(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((f &lt;span style="color: #008000;"&gt;"block-4.json"&lt;/span&gt;))
  (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; f
    (prin1
     (json-encode `((&lt;span style="color: #008000;"&gt;"date"&lt;/span&gt; . ,(current-time-string))))
     (current-buffer)))
  f)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
block-4.json
&lt;/pre&gt;


&lt;p&gt;
We put the filename as the last variable which is returned by the block, so that we don't have to manually type it later in the next block. You know, try not to repeat yourself&amp;#x2026;
&lt;/p&gt;

&lt;p&gt;
This just shows we did write out to our file:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-sh"&gt;cat block-4.json
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
And we read the file in here, using the filename from block-4 as an input variable.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
  (insert-file-contents input)
  (format &lt;span style="color: #008000;"&gt;"We got %S in block-4"&lt;/span&gt; (json-read-from-string (buffer-string))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got "{\"date\":\"Tue Feb 12 08:51:25 2019\"}" in block-4
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org578795b" class="outline-2"&gt;
&lt;h2 id="org578795b"&gt;&lt;span class="section-number-2"&gt;7&lt;/span&gt; An appendix for data&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-7"&gt;
&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp" id="orgfc06cf8"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;json&lt;/span&gt;)
(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((f &lt;span style="color: #008000;"&gt;"appendix.json"&lt;/span&gt;))
  (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; f
    (prin1
     (json-encode `((&lt;span style="color: #008000;"&gt;"date"&lt;/span&gt; . ,(current-time-string))))
     (current-buffer)))
  f)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
appendix.json
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;


&lt;div id="outline-container-org828ee8b" class="outline-2"&gt;
&lt;h2 id="org828ee8b"&gt;&lt;span class="section-number-2"&gt;8&lt;/span&gt; Caveats&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-8"&gt;
&lt;p&gt;
Using org-mode like this is almost always finding the right tradeoffs in what is persistent, and where is it stored. Not all of the intermediate data/calculations are stored; if they are really cheap you can just run the code blocks again. If they are really small, i.e. easy for your to read in a few lines, you can store them in the document. If they are really large, you can store them in a file.
&lt;/p&gt;

&lt;p&gt;
The beauty of having everything in an org-file is you have a single file that is easy to transport. When the files get too large though, it can become impractical, e.g. emacs may slow down if you try to put thousands of lines of xml data into the buffer. Then, you have to make some decisions about what to keep, where to keep it, and in what form to keep it.
&lt;/p&gt;

&lt;p&gt;
For short projects where you only need a single compute session, having everything in memory may be fine. For longer projects, say one that is long enough you will close all the buffers, and possibly restart emacs in between working on it, then you have to make some decisions about what to save from each block so you can continue the work in the next session. Again, you have to decide what to save, where to save, and in what form.
&lt;/p&gt;

&lt;p&gt;
Once you start saving data outside the org-file, it becomes less portable, or more tricky to move the file because you need to also move all the data files to keep it intact. I have explored a concept of making an org-archive in the past, where you get a list of all files linked in the org-file, but this so far has just been worked out for some small proof of concept ideas.
&lt;/p&gt;

&lt;p&gt;
Not all languages are the same in org-mode. They do not all support sessions for example, and they may not all work like the examples here. The scimax iPython modifications do not behave like the examples above. That is probably due to bugs I have inadvertently introduced, and in the future I will try to make it work like emacs-lisp does above.
&lt;/p&gt;

&lt;p&gt;
Overall, org-mode has one of the most flexible and powerful systems for passing and reusing data in documents I have ever seen. It is not perfect, and in such a powerful system there are many unexplored or lightly traveled corners that may have hazards in them. It still seems pretty promising though.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2019 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2019/02/12/Using-results-from-one-code-block-in-another-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.2.1&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>f-strings in emacs-lisp</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2018/05/14/f-strings-in-emacs-lisp</link>
      <pubDate>Mon, 14 May 2018 17:27:42 EDT</pubDate>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[elisp]]></category>
      <guid isPermaLink="false">fy7uCq1luP3IH-u2u_WegS14a4Q=</guid>
      <description>f-strings in emacs-lisp</description>
      <content:encoded><![CDATA[


&lt;p&gt;
I am a big fan of f-strings in Python 3. They let you put variable names and expressions in a string template that get expanded to create new strings. Here is a simple example of using those:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #BA36A5;"&gt;username&lt;/span&gt; = &lt;span style="color: #008000;"&gt;'John Kitchin'&lt;/span&gt;
&lt;span style="color: #BA36A5;"&gt;somevar&lt;/span&gt; = 5**0.5
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(f&lt;span style="color: #008000;"&gt;'{username:30s}{somevar:1.2f}'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
John Kitchin                  2.24


&lt;/pre&gt;

&lt;p&gt;
String formatting in emacs-lisp is by comparison not as fun and easy. Out of the box we have:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((username &lt;span style="color: #008000;"&gt;"John Kitchin"&lt;/span&gt;)
      (somevar (sqrt 5)))
  (format &lt;span style="color: #008000;"&gt;"%-30s%1.2f"&lt;/span&gt; username somevar))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
John Kitchin                  2.24

&lt;/pre&gt;

&lt;p&gt;
That is still three lines of code, but it is ugly and hard to read like the old python code:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;'%-30s%1.2f'&lt;/span&gt; % (username, somevar))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
John Kitchin                  2.24


&lt;/pre&gt;


&lt;p&gt;
My experience has shown that this gets harder to figure out as the strings get larger, and f-strings are easier to read.
&lt;/p&gt;

&lt;p&gt;
The wonderful &lt;a href="https://github.com/magnars/s.el"&gt;'s&lt;/a&gt; library provides some salvation for emacs-lisp, if you don't want the format fields. You can refer to variables in a lexical environment like this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((username &lt;span style="color: #008000;"&gt;"John Kitchin"&lt;/span&gt;)
      (somevar (sqrt 5)))
  (&lt;span style="color: #0000FF;"&gt;s-lex-format&lt;/span&gt; &lt;span style="color: #008000;"&gt;"${username}${somevar}"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
John Kitchin2.23606797749979

&lt;/pre&gt;

&lt;p&gt;
Today, I decided to do something about this, and wrote this little macro. It is a variation on s-lex-format that introduces a slightly new syntax. You can now add an optional format field separated from the variable name by a space.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;f-string&lt;/span&gt; (fmt)
  &lt;span style="color: #036A07;"&gt;"Like `&lt;/span&gt;&lt;span style="color: #D0372D;"&gt;s-format&lt;/span&gt;&lt;span style="color: #036A07;"&gt;' but with format fields in it.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;FMT is a string to be expanded against the current lexical&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;environment. It is like what is used in `&lt;/span&gt;&lt;span style="color: #D0372D;"&gt;s-lex-format&lt;/span&gt;&lt;span style="color: #036A07;"&gt;', but has&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;an expanded syntax to allow format-strings. For example:&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;${user-full-name 20s} will be expanded to the current value of&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;the variable `&lt;/span&gt;&lt;span style="color: #D0372D;"&gt;user-full-name&lt;/span&gt;&lt;span style="color: #036A07;"&gt;' in a field 20 characters wide.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;  (let ((f (sqrt 5)))  (f-string \"${f 1.2f}\"))&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;  will render as: 2.24&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;This function is inspired by the f-strings in Python 3.6, which I&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;enjoy using a lot.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;"&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((matches (s-match-strings-all&lt;span style="color: #008000;"&gt;"${&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;(?3:&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;(?1:&lt;/span&gt;&lt;span style="color: #008000;"&gt;[&lt;/span&gt;&lt;span style="color: #008000;"&gt;^&lt;/span&gt;&lt;span style="color: #008000;"&gt;} ]+&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;)&lt;/span&gt;&lt;span style="color: #008000;"&gt; *&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;(?2:&lt;/span&gt;&lt;span style="color: #008000;"&gt;[&lt;/span&gt;&lt;span style="color: #008000;"&gt;^&lt;/span&gt;&lt;span style="color: #008000;"&gt;}]*&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;)&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;\\&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold;"&gt;)&lt;/span&gt;&lt;span style="color: #008000;"&gt;}"&lt;/span&gt; fmt))
         (agetter (&lt;span style="color: #0000FF;"&gt;cl-loop&lt;/span&gt; for (m0 m1 m2 m3) in matches
                        collect `(cons ,m3  (format (format &lt;span style="color: #008000;"&gt;"%%%s"&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (string= ,m2 &lt;span style="color: #008000;"&gt;""&lt;/span&gt;)
                                                                      (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; s-lex-value-as-lisp &lt;span style="color: #008000;"&gt;"S"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"s"&lt;/span&gt;)
                                                                   ,m2))
                                                  (symbol-value (intern ,m1)))))))

    `&lt;span style="color: #D0372D;"&gt;(s-format ,fmt &lt;/span&gt;'aget (list ,@agetter))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
f-string

&lt;/pre&gt;

&lt;p&gt;
Here it is in action.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((username &lt;span style="color: #008000;"&gt;"John Kitchin"&lt;/span&gt;)
      (somevar (sqrt 5)))
  (&lt;span style="color: #0000FF;"&gt;f-string&lt;/span&gt; &lt;span style="color: #008000;"&gt;"${username -30s}${somevar 1.2f}"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
John Kitchin                  2.24

&lt;/pre&gt;

&lt;p&gt;
It still lacks some of the capability of f-strings in python, e.g. in Python, arguments inside the template to be expanded get evaluated. The solution used above is too simple for that, since it just used a regexp and is limited to the value of variables in the lexical environment.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-ipython"&gt;&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(f&lt;span style="color: #008000;"&gt;'{5**0.5:1.3f}'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
2.236


&lt;/pre&gt;

&lt;p&gt;
Nevertheless, this simple solution matches what I do most of the time anyway, so I still consider it an improvement!
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2018 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2018/05/14/f-strings-in-emacs-lisp.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.1.13&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Caching searches using biblio and only seeing new results</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2018/04/11/Caching-searches-using-biblio-and-only-seeing-new-results</link>
      <pubDate>Wed, 11 Apr 2018 20:46:56 EDT</pubDate>
      <category><![CDATA[arxiv]]></category>
      <category><![CDATA[elisp]]></category>
      <category><![CDATA[biblio]]></category>
      <guid isPermaLink="false">KOPSYg1aBm18lX8lJYHeRtMOL74=</guid>
      <description>Caching searches using biblio and only seeing new results</description>
      <content:encoded><![CDATA[


&lt;p&gt;
In this &lt;a href="https://github.com/jkitchin/scimax/issues/196"&gt;issue&lt;/a&gt; in scimax, Robert asked if it was possible to save searches, and then to repeat them every so often and only see the new results. This needs some persistent caching of the records, and a comparison of the current search results with the previous search results.
&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://github.com/cpitclaudel/biblio.el"&gt;biblio&lt;/a&gt; provides a nice interface to searching a range of resources for bibliographic references. In this post, I will focus on arxiv. Out of the box, biblio does not seem to support this use case, but as you will see, it has many of the pieces required to achieve it. Let's start picking those pieces apart.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;biblio&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
biblio

&lt;/pre&gt;

&lt;p&gt;
Here is the first piece we need: a way to run a query, and get results back as a data structure. Here we just look at the first result.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((query &lt;span style="color: #008000;"&gt;"alloy segregration"&lt;/span&gt;)
       (backend 'biblio-arxiv-backend)
       (cb (url-retrieve-synchronously (funcall backend 'url query)))
       (results (&lt;span style="color: #0000FF;"&gt;with-current-buffer&lt;/span&gt; cb
                  (funcall backend 'parse-buffer))))
  (car results))
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;((doi . &lt;span style="color: #008000;"&gt;"10.1103/PhysRevB.76.014112"&lt;/span&gt;)
 (identifier . &lt;span style="color: #008000;"&gt;"0704.2752v2"&lt;/span&gt;)
 (year . &lt;span style="color: #008000;"&gt;"2007"&lt;/span&gt;)
 (title . &lt;span style="color: #008000;"&gt;"Modelling Thickness-Dependence of Ferroelectric Thin Film Properties"&lt;/span&gt;)
 (authors nil nil nil nil nil nil nil nil nil nil nil nil nil &lt;span style="color: #008000;"&gt;"L. Palova"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;"P. Chandra"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;"K. M. Rabe"&lt;/span&gt; nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil)
 (container . &lt;span style="color: #008000;"&gt;"PRB 76, 014112 (2007)"&lt;/span&gt;)
 (category . &lt;span style="color: #008000;"&gt;"cond-mat.mtrl-sci"&lt;/span&gt;)
 (references &lt;span style="color: #008000;"&gt;"10.1103/PhysRevB.76.014112"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"0704.2752v2"&lt;/span&gt;)
 (type . &lt;span style="color: #008000;"&gt;"eprint"&lt;/span&gt;)
 (url . &lt;span style="color: #008000;"&gt;"https://doi.org/10.1103/PhysRevB.76.014112"&lt;/span&gt;)
 (direct-url . &lt;span style="color: #008000;"&gt;"http://arxiv.org/pdf/0704.2752v2"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Next, we need a database to store the results in. I will just use a flat file database with a file for each record. The filename will be the md5 hash of the doi or the record itself. Why is that a good idea? Well, the doi is a constant, so if it exists the md5 will also be a constant. The doi itself is not a good filename in general, but the md5 is. The md5 of the record itself will be fragile to any changes, so if it has a doi, we should use it. If it doesn't and later gets one, we should see it again since that could mean it has been published. Also, if it changes because of some new version we might want to see it again. In any case, the existence of that file will be evidence we have seen that record before, and will indicate we need to remove it from the current view.
&lt;/p&gt;

&lt;p&gt;
The flat file database is not super inspired. It is modeled a little after elfeed, but other solutions might work better for large sets of records, but this approach will work fine for this post.
&lt;/p&gt;

&lt;p&gt;
Here is a function that returns nil if the record has been seen, and if not, saves the record and returns it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defvar&lt;/span&gt; &lt;span style="color: #BA36A5;"&gt;db-dir&lt;/span&gt; &lt;span style="color: #008000;"&gt;"~/.arxiv-db/"&lt;/span&gt;)

(&lt;span style="color: #0000FF;"&gt;unless&lt;/span&gt; (f-dir? db-dir) (make-directory db-dir t))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;unseen-record-p&lt;/span&gt; (record)
  &lt;span style="color: #036A07;"&gt;"Given a RECORD return it if it is unseen.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;Also, save the record so next time it will be marked seen. A&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;record is seen if we have seen the DOI or the record as a string&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;before."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((doi (cdr (assoc 'doi record)))
         (contents (&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
                     (prin1 record (current-buffer))
                     (buffer-string)))
         (hash (md5 (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; doi contents)))
         (fname (expand-file-name hash db-dir)))

    (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (f-exists? fname)
        nil
      (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; fname
        (insert contents))
      record)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
unseen-record-p

&lt;/pre&gt;

&lt;p&gt;
Now we can use that as a filter that saves records by side effect.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;scimax-arxiv&lt;/span&gt; (query)
  (&lt;span style="color: #0000FF;"&gt;interactive&lt;/span&gt; &lt;span style="color: #008000;"&gt;"Query: "&lt;/span&gt;)

  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((backend 'biblio-arxiv-backend)
         (cb (url-retrieve-synchronously (funcall backend 'url query)))
         (results (-filter 'unseen-record-p (&lt;span style="color: #0000FF;"&gt;with-current-buffer&lt;/span&gt; cb
                                              (funcall backend 'parse-buffer))))
         (results-buffer (biblio--make-results-buffer (current-buffer) query backend)))
    (&lt;span style="color: #0000FF;"&gt;with-current-buffer&lt;/span&gt; results-buffer
      (biblio-insert-results results &lt;span style="color: #008000;"&gt;""&lt;/span&gt;))
    (pop-to-buffer results-buffer)))

(scimax-arxiv &lt;span style="color: #008000;"&gt;"alloy segregation"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
#&amp;lt;buffer *arXiv search*&amp;gt;

&lt;/pre&gt;

&lt;p&gt;
Now, when I run that once I see something like this:
&lt;/p&gt;

&lt;p&gt;
&lt;img src="/media/date-11-04-2018-time-20-19-52.png"&gt;
&lt;/p&gt;


&lt;p&gt;
and if I run it again:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(scimax-arxiv &lt;span style="color: #008000;"&gt;"alloy segregation"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
#&amp;lt;buffer *arXiv search*&amp;gt;

&lt;/pre&gt;

&lt;p&gt;
Then the buffer is empty, since we have seen all the entries before.
&lt;/p&gt;


&lt;p&gt;
&lt;img src="/media/date-11-04-2018-time-20-20-37.png"&gt;
&lt;/p&gt;

&lt;p&gt;
Here are the files in our database:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-sh"&gt;ls ~/.arxiv-db/
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here are the contents of one of those files:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
 (insert-file-contents &lt;span style="color: #008000;"&gt;"~/.arxiv-db/18085fe2512e15d66addc7dfb71f7cd2"&lt;/span&gt;)
 (read (buffer-string)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
((doi) (identifier . 1101.3464v3) (year . 2011) (title . Characterizing Solute Segregation and Grain Boundary Energy in a Binary
  Alloy Phase Field Crystal Model) (authors nil nil nil nil nil nil nil nil nil nil nil nil nil Jonathan Stolle nil Nikolas Provatas nil nil nil nil nil nil nil nil nil nil nil) (container) (category . cond-mat.mtrl-sci) (references nil 1101.3464v3) (type . eprint) (url . http://arxiv.org/abs/1101.3464v3) (direct-url . http://arxiv.org/pdf/1101.3464v3))

&lt;/pre&gt;

&lt;p&gt;
So, if you need to read this in again later, no problem.
&lt;/p&gt;

&lt;p&gt;
Now, what could go wrong? I don't know much about how the search results from arxiv are returned. For example, this query returns 10 hits.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((query &lt;span style="color: #008000;"&gt;"alloy segregration"&lt;/span&gt;)
       (backend 'biblio-arxiv-backend)
       (cb (url-retrieve-synchronously (funcall backend 'url query)))
       (results (&lt;span style="color: #0000FF;"&gt;with-current-buffer&lt;/span&gt; cb
                  (funcall backend 'parse-buffer))))
  (length results))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
10

&lt;/pre&gt;

&lt;p&gt;
There is just no way there are only 10 hits for this query. So, there must be a bunch more that you get by either changing the requested number in some argument, or by using subsequent queries to get the rest of them. I don't know if there are more advanced query options with biblio, e.g. to find entries newer than the last time it was run. On the advanced search &lt;a href="https://arxiv.org/find"&gt;page&lt;/a&gt; for arxiv, it looks like there is only a by year option.
&lt;/p&gt;

&lt;p&gt;
This is still a good idea, and a lot of the pieces are here,
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2018 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2018/04/11/Caching-searches-using-biblio-and-only-seeing-new-results.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.1.6&lt;/p&gt;
]]></content:encoded>
    </item>
    <item>
      <title>Overloading mathematical operators in elisp</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/07/23/Overloading-mathematical-operators-in-elisp</link>
      <pubDate>Sun, 23 Jul 2017 14:13:41 EDT</pubDate>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[elisp]]></category>
      <guid isPermaLink="false">6wjMw9DC1Qi0d1qlZyaOfJiHcbA=</guid>
      <description>Overloading mathematical operators in elisp</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#orgccfe3c4"&gt;1. Addendum&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
In Python I am used to some simple idioms like this:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;([1, 2, 3] * 2)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;"ab"&lt;/span&gt; * 3)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
[1, 2, 3, 1, 2, 3]
ababab
&lt;/p&gt;

&lt;p&gt;
There is even such fanciness as defining operators for objects, as long as they have the appropriate dunder methods defined:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt; &lt;span style="color: #6434A3;"&gt;Point&lt;/span&gt;:
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;__init__&lt;/span&gt;(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;, x, y):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.x = x
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.y = y

&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;__str__&lt;/span&gt;(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; &lt;span style="color: #008000;"&gt;"Point ({}, {})"&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.x, &lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.y)

&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;__mul__&lt;/span&gt;(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;, a):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; Point(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.x * a, &lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.y * a)

&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;__rmul__&lt;/span&gt;(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;, a):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; Point(&lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.x * a, &lt;span style="color: #0000FF;"&gt;self&lt;/span&gt;.y * a)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   
&lt;span style="color: #BA36A5;"&gt;p&lt;/span&gt; = Point(1, 1)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(p * 2)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(3 * p)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Point (2, 2)
Point (3, 3)
&lt;/p&gt;

&lt;p&gt;
Out of the box, these things are not possible in elisp. Operators like  * in elisp only take numbers or markers. We have a few options to change this. The worst option is to simply redefine these functions. That is bad because it is not reversible. We could define new functions that have the behavior we want, but then we lose the semantic meaning of "*" that we were aiming for. A better option is to &lt;a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html"&gt;advise&lt;/a&gt; these functions. This is reversible, because you can later unadvise them. Today we look at some strategies to do this.
&lt;/p&gt;

&lt;p&gt;
We will use "around" advise because it will let us bypass the original intent of the function when we want to, or use it when we do. First, we create a function that will be the advice and add it to the * function. This first draft won't actually change the behavior of *; if all the args are numbers or markers it will simply use the original function as before.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;dash&lt;/span&gt;)

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;*--*-around&lt;/span&gt; (orig-fun &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"if every arg is a number do *, else do something else."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
   ((-every? (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x) (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; (numberp x) (markerp x))) args)
    (apply orig-fun args))))

(advice-add '* &lt;span style="color: #006FE0;"&gt;:around&lt;/span&gt; #'*--*-around)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Let's just confirm
&lt;/p&gt;
&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(* 1 2 3)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
6

&lt;/pre&gt;

&lt;p&gt;
Now, we can start modifying our function to handle some other cases. Let's do the list and string first. The * function is variadic, but in these cases it makes sense to limit to two arguments. We need two cases for each type since we can write (* 2 list) or (* list 2). We also should create a fall-through case that raises an error to alert us we can't multiply things.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;*--*-around&lt;/span&gt; (orig-fun &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"if every arg is a number do *, else do something else."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;The original behavior&lt;/span&gt;
   ((-every? (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x) (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; (numberp x) (markerp x))) args)
    (apply orig-fun args))

   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;create repeated copies of list&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (listp (first args))
         (integerp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (second args) append (copy-list (first args))))

   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (integerp (first args))
         (listp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (first args) append (copy-list (second args))))

   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Make repeated string&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (stringp (first args))
         (integerp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (second args) concat (first args)))

   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (integerp (first args))
         (stringp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (first args) concat (second args)))

   (t
    (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"You cannot * %s"&lt;/span&gt; args))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
*--*-around

&lt;/pre&gt;

&lt;p&gt;
Here is the new advice in action. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(list
 (* '(a b) 2)
 (* 2 '(c d))
 (* 2 &lt;span style="color: #008000;"&gt;"ab"&lt;/span&gt;)
 (* &lt;span style="color: #008000;"&gt;"cd"&lt;/span&gt; 2))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;(a b a b)&lt;/td&gt;
&lt;td class="org-left"&gt;(c d c d)&lt;/td&gt;
&lt;td class="org-left"&gt;abab&lt;/td&gt;
&lt;td class="org-left"&gt;cdcd&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
That captures the spirit of overloading * for lists and strings. What about that object example? We have to make some assumptions here. Python looks for an uses a dunder &lt;span class="underline"&gt;&lt;span class="underline"&gt;mul&lt;/span&gt;&lt;/span&gt; method. We will assume a double dash method (&amp;#x2013;mul&amp;#x2013;) in a similar spirit. We have to modify the advice one final time. We just add a condition to check if one of the arguments is an eieio-object, and then call the &amp;#x2013;mul&amp;#x2013; function on the arguments.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;*--*-around&lt;/span&gt; (orig-fun &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"if every arg is a number do *, else do something else."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;The original behavior&lt;/span&gt;
   ((-every? (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x) (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; (numberp x) (markerp x))) args)
    (apply orig-fun args))

   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;create repeated copies of list&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (listp (first args))
         (integerp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (second args) append (copy-list (first args))))

   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (integerp (first args))
         (listp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (first args) append (copy-list (second args))))

   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Make repeated string&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (stringp (first args))
         (integerp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (second args) concat (first args)))

   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (integerp (first args))
         (stringp (second args))
         (= 2 (length args)))
    (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below (first args) concat (second args)))

   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Handle object&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (eieio-object-p (first args))
             (numberp (second args)))
        (&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (numberp (first args))
             (eieio-object-p (second args))))
    (apply '--mul-- args))

   (t
    (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"You cannot * %s"&lt;/span&gt; args))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
*--*-around

&lt;/pre&gt;

&lt;p&gt;
Now, we can define a class and the &amp;#x2013;mul&amp;#x2013; function and show that our overloaded * function works. Note we can define two signatures of &amp;#x2013;mul&amp;#x2013; so it is not necessary to define an &amp;#x2013;rmul&amp;#x2013; in this case as it was with Python (although we still create two functions in the end).
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;eieio&lt;/span&gt;)

(&lt;span style="color: #0000FF;"&gt;defclass&lt;/span&gt; &lt;span style="color: #6434A3;"&gt;Point&lt;/span&gt; ()
  ((x &lt;span style="color: #006FE0;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt;)
   (y &lt;span style="color: #006FE0;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt;)))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;--mul--&lt;/span&gt; ((p Point) a)
  (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; (* (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt;) a) &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; (* (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt;) a)))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;--mul--&lt;/span&gt; (a (p Point))
  (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; (* (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt;) a) &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; (* (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt;) a)))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;--str--&lt;/span&gt; ((p Point))
  (format &lt;span style="color: #008000;"&gt;"Point (%s, %s)"&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt;) (&lt;span style="color: #0000FF;"&gt;oref&lt;/span&gt; p &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt;)))

(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((P (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; 1)))
  (list
   (--str-- (* P 2))
   (--str-- (* 3 P))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;Point (2, 2)&lt;/td&gt;
&lt;td class="org-left"&gt;Point (3, 3)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
That is pretty awesome. Before going on, here is how you remove the advice:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(advice-remove '* '*--*-around)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
This example has been pretty instructive. You have to handle overloading for all the intrinsic types. We did lists and strings here; you might also consider vectors. For objects, it looks like we can at least try using a generic method like &amp;#x2013;mul&amp;#x2013;. One detail I neglected to consider here is that * is natively variadic. For these special cases, we did not implement variadic versions. This isn't a feature of Python which uses infix notation, so every call is with two arguments. In some cases it might make sense to support variadic args, but that seems like a generally challenging thing to do. While (* "a" 2 3) might be expected to create a string of "aaaaaa", (* "a" 2 '(3)) doesn't make sense at all.
&lt;/p&gt;

&lt;p&gt;
It would be straightforward to extend this to other operators like '+ to concatenate strings, lists and vectors, or '- to remove chars or elements, including extensions to objects using double-dash functions like &amp;#x2013;add&amp;#x2013;, &amp;#x2013;subtract&amp;#x2013;, etc. Another nice idea might be to advise print to use &amp;#x2013;str&amp;#x2013; on objects.
&lt;/p&gt;

&lt;p&gt;
On the surface this looks useful so far. Python defines &lt;i&gt;a lot&lt;/i&gt; of dunder methods that cover all kinds of scenarios including logical comparisons, bit shifting, mod, incrementing operators, casting, comparisons, right/left operations, indexing and assignment, length and others. That would be a lot of advices. This approach is moderately tedious to expand though; you have to keep adding conditional cases. 
&lt;/p&gt;

&lt;p&gt;
An alternative to the big conditional statement used in the advice might be the use of a &lt;a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Generic-Functions.html"&gt;generic function&lt;/a&gt;. With this approach we define a generic function that just does multiplication by default. Then we define specific cases with specific signatures that are used for lists, strings, objects, etc. That is basically all our conditional above was doing, matching signatures and executing a chunk of code accordingly.
&lt;/p&gt;

&lt;p&gt;
Here is our default case that does the original behavior. We still use advice to apply the function.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;cl-defgeneric&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; (orig-fun &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"Generic multiply for when no specific case exists."&lt;/span&gt;
  (apply orig-fun args))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;*--*-around-generic&lt;/span&gt; (orig-fun &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  (apply 'generic-multiply orig-fun args))

(advice-add '* &lt;span style="color: #006FE0;"&gt;:around&lt;/span&gt; #'*--*-around-generic)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That should just work as usual for regular multiplication.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(* 1 2 3 4)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
24

&lt;/pre&gt;

&lt;p&gt;
Sure enough it does. Now, we can define a specific method for a string. We need a specialized method for each signature, e.g. pre and post multiplication.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (s string) (n integer))
  (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below n concat s))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (n integer) (s string))
  (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below n concat s))

(list
 (* &lt;span style="color: #008000;"&gt;"Ac"&lt;/span&gt; 2)
 (* 2 &lt;span style="color: #008000;"&gt;"Ad"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;AcAc&lt;/td&gt;
&lt;td class="org-left"&gt;AdAd&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
That works fine, and we did not have to modify our original advice function at all! Next the  list:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (L list) (n integer))
  (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below n append (copy-list L)))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (n integer) (L list))
  (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i from 0 below n append (copy-list L)))

(list (* '(1 2) 2)
      (* 2 '(3 4)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-right" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-right"&gt;1&lt;/td&gt;
&lt;td class="org-right"&gt;2&lt;/td&gt;
&lt;td class="org-right"&gt;1&lt;/td&gt;
&lt;td class="org-right"&gt;2&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="org-right"&gt;3&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;td class="org-right"&gt;3&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
That also works fine. Last, our class example. This should work on all objects I think (unless there is some way to make classes that do not inherit the default superclass).
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (n integer) (obj eieio-default-superclass))
  (--mul-- n obj))

(&lt;span style="color: #0000FF;"&gt;cl-defmethod&lt;/span&gt; &lt;span style="color: #006699;"&gt;generic-multiply&lt;/span&gt; ((orig-fun subr) (obj eieio-default-superclass) (n integer))
  (--mul-- n obj))

(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((P (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; 1)))
  (list
   (--str-- (* P 2))
   (--str-- (* 3 P))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;Point (2, 2)&lt;/td&gt;
&lt;td class="org-left"&gt;Point (3, 3)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
This is a much better approach to extending the multiplication operator! If I continue this path in the future I would probably take this one.  This could be useful to make elisp more like some more popular contemporary languages like Python, as well as to add linear algebra like notation or mathematical operations on objects in elisp. It kind of feels like these operations ought to be generic functions to start with to make this kind of overloading easier from the beginning.  Functions like "*" are currently defined in the C source code though, maybe for performance reasons. It is not obvious what the consequences of making them generic might be.
&lt;/p&gt;

&lt;div id="outline-container-orgccfe3c4" class="outline-2"&gt;
&lt;h2 id="orgccfe3c4"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Addendum&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
Christopher Wellons &lt;a href="http://disq.us/p/1kr76r9"&gt;pointed out&lt;/a&gt; an important limitation of advice: they don't work on byte-compiled functions. Let's see what he means. Here is a simple function that will just multiply a Point object by an integer:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;to-be-bytten&lt;/span&gt; (p1 n)
  (* p1 n))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
to-be-bytten

&lt;/pre&gt;

&lt;p&gt;
Here it is in action, and here it works fine.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(to-be-bytten (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; 1) 2)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[eieio-class-tag--Point 2 2]

&lt;/pre&gt;

&lt;p&gt;
Now, let's byte-compile that function and try it again:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(byte-compile 'to-be-bytten)

(&lt;span style="color: #0000FF;"&gt;condition-case&lt;/span&gt; err
    (to-be-bytten (Point &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:y&lt;/span&gt; 1) 2)
  ((&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; r)
   (message &lt;span style="color: #008000;"&gt;"Doh! Christopher was right. It did not work...\n%s"&lt;/span&gt; err)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Doh! Christopher was right. It did not work...
(wrong-type-argument number-or-marker-p [eieio-class-tag--Point 1 1])

&lt;/pre&gt;

&lt;p&gt;
So the advice is pretty limited since most of the functions in Emacs core are likely to be byte-compiled, and it might mean you have to redefine * completely, or define some new function that looks like it. Too bad, the advice was pretty easy! 
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/07/23/Overloading-mathematical-operators-in-elisp.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.7&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>A callable plist data structure for Emacs</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/04/16/A-callable-plist-data-structure-for-Emacs</link>
      <pubDate>Sun, 16 Apr 2017 16:44:53 EDT</pubDate>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[macro]]></category>
      <category><![CDATA[elisp]]></category>
      <guid isPermaLink="false">g-L-SDSuJKkcZwHQyGqqiWpzU7k=</guid>
      <description>A callable plist data structure for Emacs</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#orgb831a3d"&gt;1. An update &lt;span class="timestamp-wrapper"&gt;&lt;span class="timestamp"&gt;&amp;lt;2017-04-21 Fri&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Emacs lisp has a few data structures that store key-value pairs. Here are some canonical examples of these data structures and the way to get data out of them.
&lt;/p&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;a-lists&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((data '((key1 . 4)
              (key2 . &lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;))))
  (cdr (assoc 'key2 data)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
tree

&lt;/pre&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;p-lists&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((data '(&lt;span style="color: #006FE0;"&gt;:key1&lt;/span&gt; 4 &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt; &lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;)))
  (plist-get data &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
tree

&lt;/pre&gt;

&lt;ul class="org-ul"&gt;
&lt;li&gt;A hash table&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((data #s(hash-table data (key1 4 key2 &lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;))))
  (gethash 'key2 data))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
tree

&lt;/pre&gt;

&lt;p&gt;
Each of these uses some function to get data out of them. I have been learning about closures today, and realized a way you can make a "callable" data structure using them. In a closure, the data is stored as part of a function. We will use a &lt;a href="http://letoverlambda.com"&gt;"let over lambda"&lt;/a&gt; with a defalias in a lexical environment to achieve this. I will wrap a p-list with this approach, but it could work with any of the examples above. We will make the function have a few behaviors that allow us to see the whole data structure with no args, to get a value with one arg that is a key, and to set a value if there are more than two args add them as key-val pairs to the data structure. This block binds the function to the symbol "d" which is then a callable function.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((data '(&lt;span style="color: #006FE0;"&gt;:key1&lt;/span&gt; 4 &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt; &lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;)))
  (&lt;span style="color: #0000FF;"&gt;defalias&lt;/span&gt; '&lt;span style="color: #006699;"&gt;d&lt;/span&gt;
    (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; key-vals)
      (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
       &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;no args, return data&lt;/span&gt;
       ((= 0 (length key-vals))
        data)
       &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;just a key, get val&lt;/span&gt;
       ((= 1 (length key-vals))
        (plist-get data (car key-vals)))
       (t
        (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for key in (-slice key-vals 0 nil 2)
              for val in (-slice key-vals 1 nil 2)
              do
              (plist-put data key val))
        data)))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
d

&lt;/pre&gt;

&lt;p&gt;
Now we can use it like to get some data out:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
tree

&lt;/pre&gt;

&lt;p&gt;
And add new values like:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d &lt;span style="color: #006FE0;"&gt;:key3&lt;/span&gt; &lt;span style="color: #008000;"&gt;"oak"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;:key1&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;td class="org-left"&gt;:key2&lt;/td&gt;
&lt;td class="org-left"&gt;tree&lt;/td&gt;
&lt;td class="org-left"&gt;:key3&lt;/td&gt;
&lt;td class="org-left"&gt;oak&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
You can update a value with this too:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d &lt;span style="color: #006FE0;"&gt;:key3&lt;/span&gt; &lt;span style="color: #008000;"&gt;"pine"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;:key1&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;td class="org-left"&gt;:key2&lt;/td&gt;
&lt;td class="org-left"&gt;tree&lt;/td&gt;
&lt;td class="org-left"&gt;:key3&lt;/td&gt;
&lt;td class="org-left"&gt;pine&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
or add multiple values like this:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d &lt;span style="color: #006FE0;"&gt;:key4&lt;/span&gt; 0 &lt;span style="color: #006FE0;"&gt;:key5&lt;/span&gt; 9)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;:key1&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;td class="org-left"&gt;:key2&lt;/td&gt;
&lt;td class="org-left"&gt;tree&lt;/td&gt;
&lt;td class="org-left"&gt;:key3&lt;/td&gt;
&lt;td class="org-left"&gt;pine&lt;/td&gt;
&lt;td class="org-left"&gt;:key4&lt;/td&gt;
&lt;td class="org-right"&gt;0&lt;/td&gt;
&lt;td class="org-left"&gt;:key5&lt;/td&gt;
&lt;td class="org-right"&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
And see the whole plist with no args:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;:key1&lt;/td&gt;
&lt;td class="org-right"&gt;4&lt;/td&gt;
&lt;td class="org-left"&gt;:key2&lt;/td&gt;
&lt;td class="org-left"&gt;tree&lt;/td&gt;
&lt;td class="org-left"&gt;:key3&lt;/td&gt;
&lt;td class="org-left"&gt;pine&lt;/td&gt;
&lt;td class="org-left"&gt;:key4&lt;/td&gt;
&lt;td class="org-right"&gt;0&lt;/td&gt;
&lt;td class="org-left"&gt;:key5&lt;/td&gt;
&lt;td class="org-right"&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Pretty nice! It seems like there ought to be a macro to facilitate creating those. Here is one.
This macro basically expands to the same code as above, but for fun I add a default value option.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;default-dict&lt;/span&gt; (var &lt;span style="color: #6434A3;"&gt;&amp;amp;optional&lt;/span&gt; default &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; key-vals)
  &lt;span style="color: #036A07;"&gt;"Bind a callable plist to VAR that contains KEY-VALS."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ()
    `&lt;span style="color: #D0372D;"&gt;(let ((data &lt;/span&gt;',key-vals))
       (&lt;span style="color: #0000FF;"&gt;defalias&lt;/span&gt; ',var
         (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; key-vals)
           (message &lt;span style="color: #008000;"&gt;"%s"&lt;/span&gt; key-vals)
           (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
            &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;no args, return data&lt;/span&gt;
            ((= 0 (length key-vals))
             data)
            &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;just a key, get val&lt;/span&gt;
            ((= 1 (length key-vals))
             (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt;  (plist-get data (car key-vals)) ,default))
            (t
             (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for key in (-slice key-vals 0 nil 2)
                   for val in (-slice key-vals 1 nil 2)
                   do
                   (plist-put data key val))
             data)))))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is an instance of it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(default-dict d2 &lt;span style="color: #008000;"&gt;"None"&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:key1&lt;/span&gt; 4 &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt; &lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
d2

&lt;/pre&gt;

&lt;p&gt;
And here it is in use.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d2 &lt;span style="color: #006FE0;"&gt;:key1&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
4

&lt;/pre&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(d2 &lt;span style="color: #006FE0;"&gt;:new-key&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
None

&lt;/pre&gt;

&lt;p&gt;
Not bad. If you come from Python, you might find this style of data structure to be more similar to what you are used to seeing. It sure seems less verbose than the usual plist boilerplate I have used before.
&lt;/p&gt;

&lt;div id="outline-container-orgb831a3d" class="outline-2"&gt;
&lt;h2 id="orgb831a3d"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; An update &lt;span class="timestamp-wrapper"&gt;&lt;span class="timestamp"&gt;&amp;lt;2017-04-21 Fri&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
One (perhaps undesirable even) feature of the approach above is that it creates a function in the global namespace. This might have unintended consequences with name clashes or shadowing, and if you later use the same variable name for a plist, you would change the function behavior. Here we consider a way to limit the scope of where these functions exist and work. The labels macro provides one way to do this, we just create temporary functions that only exist within a scope. There is a lot of backticking and comma operators in this, and it took quite a few iterations to get it working!
&lt;/p&gt;

&lt;p&gt;
This macro creates temporary functions for each keyword that return the value in the plist.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;with-dict&lt;/span&gt; (key-vals &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; body)
  &lt;span style="color: #036A07;"&gt;"A context-manager for a plist where each key is a callable&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;function that returns the value."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;declare&lt;/span&gt; (indent 1))
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((g (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (symbolp key-vals)
                (symbol-value key-vals)
              key-vals))
         (keys (-slice g 0 nil 2)))
    `(&lt;span style="color: #0000FF;"&gt;labels&lt;/span&gt; ,(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for key in keys
                    collect
                    (list key '() `&lt;span style="color: #D0372D;"&gt;(plist-get &lt;/span&gt;',g  ,key)))
       ,@body)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
with-dict

&lt;/pre&gt;

&lt;p&gt;
Here is how we use it:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(with-dict (&lt;span style="color: #006FE0;"&gt;:a&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:b&lt;/span&gt; 'some-symbol &lt;span style="color: #006FE0;"&gt;:c&lt;/span&gt; 3)
  (&lt;span style="color: #006FE0;"&gt;:b&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;quote&lt;/td&gt;
&lt;td class="org-left"&gt;some-symbol&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
We can also use it with variables that hold mappings like this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((d '(&lt;span style="color: #006FE0;"&gt;:key1&lt;/span&gt; 1 &lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt; some-other-symbol &lt;span style="color: #006FE0;"&gt;:key3&lt;/span&gt; 3)))
  (with-dict d
    (format &lt;span style="color: #008000;"&gt;"We got %s"&lt;/span&gt; (&lt;span style="color: #006FE0;"&gt;:key2&lt;/span&gt;))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
We got some-other-symbol

&lt;/pre&gt;

&lt;p&gt;
That is pretty interesting! In case that looks similar to a context manager in Python, now you know where Python got that idea ;)
&lt;/p&gt;

&lt;p&gt;
Another related idea is to let-bind the values to variables within a scope. We can't use the keywords directly here, so I use some hackery to strip off the colon so it is a regular symbol. That is not quite as nice I guess since you have to remember to remove the : from the symbols in the body of your code.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;with-plist-vals&lt;/span&gt; (plist &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; body)
  &lt;span style="color: #036A07;"&gt;"Bind the values of a plist to variables with the name of the keys."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;declare&lt;/span&gt; (indent 1))
  `(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ,(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for key in (-slice plist 0 nil 2)
               for val in (-slice plist 1 nil 2)
               collect (list (intern
                              (substring (symbol-name key) 1))
                             val))
     ,@body))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
with-plist-vals

&lt;/pre&gt;

&lt;p&gt;
Here is an example usage.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(with-plist-vals (&lt;span style="color: #006FE0;"&gt;:a&lt;/span&gt; 4 &lt;span style="color: #006FE0;"&gt;:b&lt;/span&gt; 6)
 (* 2 a))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
8

&lt;/pre&gt;

&lt;p&gt;
Obviously that is just an alternate syntax for the let statement, but it lets you leverage the plist syntax for multiple purposes.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2018 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/04/16/A-callable-plist-data-structure-for-Emacs.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.1.6&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>A better defun for emacs-lisp</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/03/22/A-better-defun-for-emacs-lisp</link>
      <pubDate>Wed, 22 Mar 2017 16:30:33 EDT</pubDate>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[macro]]></category>
      <category><![CDATA[elisp]]></category>
      <guid isPermaLink="false">RoQ2wpVtnKbGMQqdpFrDLjXCPWw=</guid>
      <description>A better defun for emacs-lisp</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#org20fd352"&gt;1. Summary&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
I have been thinking of better ways to write code that is more likely to have decent docstrings that are up to date, and maybe that enable automatic validation. One strategy is to keep documentation and code together, and by together I mean &lt;i&gt;close together&lt;/i&gt;. The closer the better.  I made some interesting progress in the &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/03/19/A-Hy-macro-for-defining-functions-with-docstrings-on-each-argument/"&gt;last post&lt;/a&gt;, where I used a macro to let me put argument specific documentation in the same place that the argument is defined. Here I expand the idea to also provide argument default values, and validation code where the argument is defined inside the function, in addition to generating docstrings. This post is written in Emacs-lisp, mostly because I am more familiar with the macro language. The idea should apply to other lisps too.
&lt;/p&gt;

&lt;p&gt;
Let's consider this prototypical, vanilla function definition, usage, and docstring.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;f1&lt;/span&gt; (arg1 arg2)
  &lt;span style="color: #036A07;"&gt;"Add two numbers."&lt;/span&gt;
  (+ arg1 arg2))

&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;usage&lt;/span&gt;
(f1 3 4)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
7

&lt;/pre&gt;

&lt;p&gt;
Here is what the help looks like from emacs.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(describe-function 'f1)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
f1 is a Lisp function.

(f1 ARG1 ARG2)

For more information check the manuals.

Add two numbers.

&lt;/pre&gt;

&lt;p&gt;
It is clear I was lazy in writing the docstring; it does not even mention the arguments. There is also no validation of the arguments so if you pass a string and a number, you will get an error. There are no defaults either, so you have to provide both arguments. It seems like there could be significant room for improvement. Of course, I could bite the bullet and write a better function like this one:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;f1a&lt;/span&gt; (arg1 &lt;span style="color: #6434A3;"&gt;&amp;amp;optional&lt;/span&gt; arg2)
  &lt;span style="color: #036A07;"&gt;"Add ARG1 and ARG2 together.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;ARG1 and  ARG2 should both be numbers."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (null arg2) (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; arg2 2))
  (&lt;span style="color: #0000FF;"&gt;unless&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (numberp arg1) (numberp arg2)) (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"arg1 and arg2 should both be numbers"&lt;/span&gt;))
  (+ arg1 arg2))

(list (f1a 3 4) (f1a 3))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-right" /&gt;

&lt;col  class="org-right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-right"&gt;7&lt;/td&gt;
&lt;td class="org-right"&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Yes, I could do that, but it is tedious to do it all the time. And it still leaves something to be desired for me. The docstring does not say what the default value is for example, and that is hard-coded in the code, i.e. not introspectible until you look at the code.  Next we consider an alternative way to write the function. Compare that to this function definition, usage and documentation. The function definition is a little more verbose. Providing documentation, defaults and validation code in any form would make it that way no matter what.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(defn f2 ((arg1 &lt;span style="color: #008000;"&gt;"A number"&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:validate&lt;/span&gt; numberp)
          (arg2 &lt;span style="color: #008000;"&gt;"A number"&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:validate&lt;/span&gt; numberp &lt;span style="color: #006FE0;"&gt;:default&lt;/span&gt; 2))
  &lt;span style="color: #008000;"&gt;"Add the arguments."&lt;/span&gt;
  (+ arg1 arg2))

&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;usage&lt;/span&gt;
(list (f2 3 4) (f2 3))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-right" /&gt;

&lt;col  class="org-right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-right"&gt;7&lt;/td&gt;
&lt;td class="org-right"&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(describe-function 'f2)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
f2 is a Lisp function.

(f2 ARG1 &amp;amp;optional ARG2)

For more information check the manuals.

Add the arguments.
ARG1 : A number (valid = numberp)
ARG2 : A number (default = 2) (valid = numberp)

&lt;/pre&gt;

&lt;p&gt;
The documentation is built up from the information in the function definition, in a form that is mostly consistent with emacs-lisp documentation standards. &lt;code&gt;defn&lt;/code&gt; is not a regular emacs-lisp function; it is a macro I developed to generate the function code. It turned out to be long, but the gist of it is that before defining the function I loop through the arguments and collect the docstrings, along with any information about default values and/or validation functions. Then I build up the list of arguments to put in the function. Then if any default values are set, I generate some code to set those values if they are not set in the function call, and finally a similar block of validation code. At the end, I construct the defun and return it. You can check out the code if you want here: &lt;a href="https://github.com/jkitchin/scimax/blob/master/scimax-macros.el"&gt;https://github.com/jkitchin/scimax/blob/master/scimax-macros.el&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Let's take a look at what this code expands to.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(macroexpand-1
 '(defn f2 ((arg1 &lt;span style="color: #008000;"&gt;"A number"&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:validate&lt;/span&gt; numberp)
            (arg2 &lt;span style="color: #008000;"&gt;"A number"&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;:validate&lt;/span&gt; numberp &lt;span style="color: #006FE0;"&gt;:default&lt;/span&gt; 2))
    &lt;span style="color: #008000;"&gt;"Add the arguments."&lt;/span&gt;
    (+ arg1 arg2)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;f2&lt;/span&gt;
    (arg1 &lt;span style="color: #6434A3;"&gt;&amp;amp;optional&lt;/span&gt; arg2)
  &lt;span style="color: #036A07;"&gt;"Add the arguments.\nARG1 : A number (valid = numberp)\nARG2 : A number (default = 2) (valid = numberp)\n"&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;progn&lt;/span&gt;
    (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt;
        (null arg2)
      (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; arg2 2)))
  (&lt;span style="color: #0000FF;"&gt;progn&lt;/span&gt;
    (&lt;span style="color: #0000FF;"&gt;unless&lt;/span&gt;
        (funcall 'numberp arg1)
      (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"In (%s %s) Expected %s to pass %S. Got %S"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"f2"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"(arg1 &amp;amp;optional arg2)"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"arg1"&lt;/span&gt; 'numberp arg1))
    (&lt;span style="color: #0000FF;"&gt;unless&lt;/span&gt;
        (funcall 'numberp arg2)
      (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt; &lt;span style="color: #008000;"&gt;"In (%s %s) Expected %s to pass %S. Got %S"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"f2"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"(arg1 &amp;amp;optional arg2)"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"arg2"&lt;/span&gt; 'numberp arg2)))
  (+ arg1 arg2))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
You can see it expands to a regular defun, with a generated docstring, generated default settings code block, and generated validation code. Pretty nice.
&lt;/p&gt;

&lt;p&gt;
Let's see what happens with a function that fails the validation. We should get an error. Here we capture the error so we can see it in the post.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;condition-case&lt;/span&gt; err
    (f2 &lt;span style="color: #008000;"&gt;"oak"&lt;/span&gt;)
  (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt;
   (error-message-string err)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
In (f2 (arg1 &amp;amp;optional arg2)) Expected arg1 to pass numberp. Got "oak"

&lt;/pre&gt;

&lt;p&gt;
So we even get a useful error message when the wrong type of argument is provided. Compare that to the error message from the original version of this function. It tells us we got the wrong type, but not which argument.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;condition-case&lt;/span&gt; err
    (f1 &lt;span style="color: #008000;"&gt;"oak"&lt;/span&gt; 4)
  (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt;
   (error-message-string err)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Wrong type argument: number-or-marker-p, "oak"

&lt;/pre&gt;

&lt;p&gt;
One last example to check out the &amp;amp;rest argument, with validation that every arg is a number.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(defn f4 ((rarg &lt;span style="color: #006FE0;"&gt;:rest&lt;/span&gt;
                &lt;span style="color: #006FE0;"&gt;:validate&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x)
                            (-all-p 'identity (mapcar 'numberp x)))))
  &lt;span style="color: #008000;"&gt;"multiply all the arguments."&lt;/span&gt;
  (apply '* rarg))

(f4 1 2 3)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
6

&lt;/pre&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;condition-case&lt;/span&gt; err
    (f4 &lt;span style="color: #008000;"&gt;"oak"&lt;/span&gt; 4)
  (&lt;span style="color: #ff0000; font-weight: bold;"&gt;error&lt;/span&gt;
   (error-message-string err)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
In (f4 (&amp;amp;rest rarg)) Expected rarg to pass (lambda (x) (-all-p (quote identity) (mapcar (quote numberp) x))). Got ("oak" 4)

&lt;/pre&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(describe-function 'f4)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
f4 is a Lisp function.

(f4 &amp;amp;rest RARG)

For more information check the manuals.

multiply all the arguments.
RARG : No documentation

&lt;/pre&gt;

&lt;p&gt;
That looks ok too.
&lt;/p&gt;

&lt;div id="outline-container-org20fd352" class="outline-2"&gt;
&lt;h2 id="org20fd352"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Summary&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
The motivation for this was to help me write better code with better documentation. Better code in the sense that it can provide run-time validation, with better feedback, and automatic documentation, including that there is none if that is the case. It is basically compatible with the regular defun, but enhances what kind of documentation is possible with less work on my part. I think it will make it easier to keep documentation in sync, since the argument documentation would be kept near the argument, and you can build in validation if you want to.
&lt;/p&gt;

&lt;p&gt;
It is no news to lispers that macros are good for this kind of application.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/03/22/A-better-defun-for-emacs-lisp.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.7&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
