Literate Programming for Programming Notes

Over at Arcadian Visions there’s a nice post on using literate programming techniques for making programming notes. The specific problem the post addresses is how to effectively take notes on some piece of programming arcana that you will want to remember later or perhaps share with a colleague. Part of the requirements is to have working code embedded with the note.

An excellent first order solution to this is, of course, Org mode and Babel and I’ve written several posts about that. This poster, though, is seeking a more “reproducible research-like” solution. He wants to be able to reproduce the original solution exactly as he performed it the first time: same code, same libraries, same everything. To restore the original environment he leverages direnv and nix. There is even an Emacs interface to direnv to make things easier.

Even if you don’t need or want to restore your original environment, the post is still worth reading for the Babel examples. Usually such examples are pretty simple but here the examples are more real world and use C++ and Haskell as the target languages. It even demonstrates how to teach Org to run a “new” language.

Anyone who uses or want to use Org Babel should read this post. It’s especially useful for anyone who wants to use Emacs and Org for reproducible research.

Posted in General | Tagged , | Leave a comment

Whitespace and Regexes in Emacs

I’ve always found that specifying whitespace in an Emacs regex was harder than it should be. If I want anything more than a simple space (or spaces) I always have a difficult time remembering the incantation. Over time, I’ve settled on \s-, which seems to work in every case I’m interested in.

Marcin Borkowski (mbork) had similar difficulty and has a great post on what he’s discovered. The situation is more complicated than you might think but it all makes sense once you understand what’s going on. The TL;DR is that in almost every case, \s- is indeed what you want but there are some edge cases worth knowing about. Take a look at Borkowski’s post to see what they are.

Posted in General | Tagged | Leave a comment

Being Organized with Org Mode

Over at the Emacs subreddit, rmberYou has a post on how he uses Org mode to get and stay organized. The post is mainly a taxonomy of his Org mode files but his setup may be useful to others.

A couple of points are worth mentioning. First, he prefers a single large Org file for each general category. That’s the approach taken by Carsten Dominik1, the original developer of Org mode. I like that approach because every thing related to a given category is one place. This is especially helpful when you’re employing reproducible research methods.

Secondly, one of the commentors notes that you should also integrate a knowledge system to record and help you remember bits of knowledge even if they aren’t actionable. I do this by recording things I’ve learned and might want later in my journal. Org mode is really good for this because of its tag system. I can simply add tags to the entry that reflect its content and then when I want it again, it’s easy to find if I can remember even one of the tags.

It’s worth spending a couple of minutes to read rmberYou’s post. He says being organized helps you deal with today’s hectic pace. I’ve found that a little effort in being organized allows me to get things done while still indulging my basically lazy nature.

Footnotes:

1

Actually, I believe Dominik uses (or used) just a single Org file for everything.

Posted in General | Tagged | Leave a comment

Magit from the Terminal

For those of you who like to work in the terminal, UnwashedMeme over at the Emacs subreddit has a handy tip. It’s an alias that brings up an Emacs buffer with the Magit status buffer. This doesn’t enable any new functionality, of course. It just means you can type magit and immediately have a Magit buffer rather than starting emacsclient and then starting Magit. The comments have some variations on the theme so be sure to read those too.

Posted in General | Tagged , | Leave a comment

Copy Sexp at Mouse Position to Point

Via Wilfred Hughes, I found this old post to the gnu-emacs-sources mailing list giving a quick and dirty implementation of an old Lisp Machine trick: copying a sexp that the mouse is pointing at to the position at point. Vladimir Sedach took that code and fleshed it out to a more complete implementation.

If you work with some version of a Lisp language this may be a useful addition to your toolkit. Yes, it forces you to use your mouse, which many Emacsers are loathe to do, but only in a minimal way. As Sedach says, this is often faster than repositioning the point to copy the sexp. That’s especially true if the sexp is in another window. Sedach’s method works across windows so it’s easy to copy a sexp from, say, another file as long as it’s currently displayed in Emacs.

This bit of functionality comes from the Lisp Machine so folks earning their living writing Lisp code found it worthwhile. Granted, the Lisp Machine was more mouse centered than Emacs tends to be but this seems worthwhile enough to make an exception. After all, it’s not like we’re going to be using it several times a minute.

The code does not appear to be available as an ELPA package so you’ll have to get it yourself from GitHub.

Posted in General | Tagged | Leave a comment

Why Emacs Is Awesome

A few months ago I wrote about Luke Smith’s challenge for others to make a video convincing him to use Emacs. Among those who responded was JimKD who produced a very nice video that showcased some of the great features of Emacs. I just saw a reference to the video and watched it again. I vaguely remembered seeing it before but couldn’t remember if I’d written about it until I searched my Irreal archive. Since it was worth watching again and since there are new pointers to it, I decided I’d revisit it.

Smith is (or was) getting ready to write his thesis so one of the things he was looking for in an editor was features that made writing easy. JimKD emphasized that aspect of Emacs in his video. He begins by bringing up a buffer containing a \(\mathrm{\LaTeX}\) document and shows how it is easy to navigate to different parts of the document, show and operate on two views of the document, and compile or view the document as a PDF file. When I’m using a markup language to write, I find I often want to check the result to ensure that the markup produced the result I wanted so the preview function is important to me and probably most folks using non-trivial markup.

JimKD then moves to Org mode. His discussion is mostly focused on Org’s table editor. He shows how an Org table can behave like a spreadsheet while still being in plain text. JimKD emphasizes how important that plain text aspect is: you can always read your data even if the software that produced it disappears for some reason.

The video is just over 25 minutes but it’s interesting and well worth the time to watch it. If you haven’t seen it or haven’t seen it for a while, take a look.

Posted in General | Tagged | Leave a comment

How A Nerd Counts Stairs

Tanya Khovanova recounts an amusing trick she learned from John Conway: how to count the number of steps in a stairway. The obvious answer is to just count them as you walk up the stairs but there’s an additional stipulation: count them like a nerd would.

The basic idea is to first determine the number of step modulo 10 and then estimate the how many groups of 10 steps there are to determine the total number. I’ll let you read Khovanova’s post to see the trick for finding the steps mod 10.

There’s not really anything exciting here but it did strike my fancy for some reason. Perhaps you’ll enjoy it too.

Posted in General | Tagged | Leave a comment

Refreshing the Occur Buffer

Over at the Emacs subreddit, attrigh asks if there is some way of refreshing the results of an occur when the underlying buffer changes. I’ve never run into a use case where that would be helpful but it’s easy to see how it could in many workflows. I didn’t know the answer off the top of my head but I probably would have stumbled on the answer just by doing the expected thing if I had needed an occur refresh.

In any event, the answer is to type g just as you do in similar situations to rebuild the results buffer of some command. I doubt this tip is going to save anyone hours of time but it is a handy thing to know for when your underlying buffer changes and you really don’t want to retype that long, nasty regex.

Posted in General | Tagged | Leave a comment

Actionable Links in Emacs Buffers

As most of you know, I do almost all my writing in Org mode so I’m used to being able to follow a URL by typing return on it or with Ctrl+c Ctrl+o. I use this all the time and, occasionally, when I’m in some other type of buffer I’ll try to follow the link and it won’t work. I end up copying the link and pasting it into my browser. That’s a real cache miss for me.

I just had another of those “how did I not know this” moments when I saw Álvaro Ramírez’s post about enabling the exact functionality I wanted. It’s been there all along. I’m not sure when it was integrated into Emacs core but the creation date on the source file is 1995.

Don’t let Ramírez’s use of use-package mislead you. It’s not a third-party package; it’s built into Emacs. On the other hand, Ramírez’s use-package invocation is useful because it shows you how to set it up. For most buffer types, you’ll want to use goto-address-mode but for code you’ll probably want goto-address-prog-mode which behaves the same as goto-address-mode but only for strings and comments. If you use Org buffers much of the time, you may want to bind Ctrl+c Ctrl+o instead of or in addition to Return.

This is a great find for me. I don’t need it that often but it was really annoying when I did and I didn’t know the easy way of doing it. Thanks to Ramírez for the tip. Perhaps you’ll find it useful too.

Posted in General | Tagged | Leave a comment

Splitting An Org Block

Kaushal Modi has a nice post on how to split an Org block. That comes up in the context of, say, writing a literate program and deciding you want to insert some commentary in the middle of the block. A simple keypress and the block is split in two and the cursor is positioned between the two parts.

The notion of “block” is general. The splitting will work with any type of Org block so its use extends beyond the simple case above. You wouldn’t think this would be too complicated but, in fact, it’s a bit fiddly. There’s no built-in predicate to tell you when you’re inside any Org block so Modi had write that first. The rest is mostly diddling around with regexes but there’s still the problem of deciding where to split the block.

Modi shows all his code and has a nice commentary on what he’s doing so it’s easy to follow along. There’s also a pointer to his setup-org.el file where you can see all the code at once. This is a nice post and Modi’s code may be useful to those of you who frequently use Org blocks in your writing. It’s also another wonderful example of how Emacs lets you scratch just about any itch.

Posted in General | Tagged , | Leave a comment