Part 1: Using Org to Develop Elisp
This post is part of a series.
- (You are here) Part 1: Using Org to Develop E-lisp snippets
- Part 2: Counting Words
One of the powers of Org mode is the ability to run blocks of code and collect the output. This allows you to capture code, context for writing the code (as actual text), and results in a single document. This blog-post will describe how I’ve started to learn Emacs lisp using org and Emacs itself.
In Emacs, there are many ways to trigger the same actions.
I find that memorizing chords is terribly difficult (if I have any memorized its through muscle memory rather than my brain knowing what I’m typing).
Here is a short table of commands I’ve needed to get at the documentation builtin to Emacs.
Usually I run these via
M-x FUNCTION, which allows me to get at tab-completion if I don’t remember the exact name.
||show doc page for function|
||show manual page for function|
||show doc page for a chord|
||show manual page for chord|
||show GNU Emacs FAQ|
||Bring up info documentation browser to the last info page you were looking at|
||Shortcut to ELisp documentation|
||Top level info directory|
||Great function name. Does a bunch of things, namely running the source code block|
||Edit block/table in a separate buffer popup|
Helpful web pages:
- Orgmode Source Code Editing
- Org Easy Templates
- Org Source Block Header Arguments
- GNU Manual on Lisp in Emacs and the page on save-excursion
The Power of Introspection
I do not learn through text books. Often, I find that they have all this extra narrative, when all I really want is the equation or detailed function documentation. This does make it somewhat difficult for me to learn new programming languages as I’m not going to sit down a read a book. This is where an IDE can really help.
In psychology, the process of introspection relies exclusively on observation of one’s mental state. Introspection is closely related to human self-reflection and is contrasted with external observation1.
In programming, introspection is the ability to look at the code itself, as a program is running.
Emacs allows this (in a sense) through the
Users can view both documentation and even the source code itself2.
describe-function function, along with tab completion, you can guess at functions that may exists and start to build e-lisp snippets.
Built-in documentation isn’t technically introspection, but it is nice.
To bring up the “Emacs Lisp” manual, type
C-h i m elisp RET.
Despite my dislike of narrative, I now present a basic walk-through for getting started with Org mode code blocks.
Starting with Org Blocks
Now that we know how to bring up documentation without leaving Emacs, lets code! I’m sure we could create a new elisp file / project, but who has time? I would rather do my day job and learn a little new elisp when the opportunity arises. This means I’m already working on something while writing in Org-mode.
Basics: Running and Results
Org source blocks contain snippets of code that can be executed.
First, make sure executing e-lisp code through Org babel in enabled (usually in your
Let’s start with a very basic Emacs lisp snippet.
<s TAB will automatically create a source code block (this is called a template3).
Placing the cursor within the block at hitting
C-c C-c triggers the run (type
y to allow the snippet to run on your computer).
The results appear in the document.
Elisp isn’t my go-to calculator, but it is a handy way to run simple calculations. Let’s get more complicated with some quick text parsing. Here I create 2 lists of items, then print all the permutations through an Org block.
A few notes about this example:
- To edit the e-lisp code in an “Emacs-Lisp” mode buffer, move the cursor in to the source block and type
C-c '. This ensures proper indentation and other major mode niceness. Note that this can work for any major mode4, including text.
- Notice the
:results outputheader argument on the code block5. There are a bunch of arguments, but the results one always messes me up. In this case, I want to print the output as the snippet executes, not the return value of the snippet.
- Do not forget to
cdryour while loop variable. Should you forget, use
C-gto kill the execution.
- Emacs print functions are weird and the documentation is obtuse… I just use
princ. Unfortunately there is no easy to
princmany items in a single, variable argument command (and I don’t want figure out how to build strings).
1 Introspection. Wikipedia, The Free Encyclopedia. Accessed Jan. 4, 2018.
2 Okay, this isn’t technically programmatic introspection, more like “built-in documentation.” On the other hand, Emacs is somewhat unique in how users can interact with the documentation and the underlying code.
3 Documentation for Org Easy Templates
4 Documentation for Org source blocks
5 Documentation for Org source block header arguments
Comments are sent directly to me and may or may not be posted for display on this page.