Building an Emacs Mode to Improve Your Workflow

Adrien Brochard gave an excellent talk to the New York Emacs Meetup in which he demonstrates how to build an Emacs major mode to improve a workflow. Brochard is working with Kubernetes and would like a way to look at—or even tail and follow—the logs. For those who don’t know what Kubernetes does, or perhaps have never heard of it, it’s a system for controlling a set of processes (called pods).

Kubernetes, of course, has a way of displaying and tailing the logs but you first have to call a Kubernetes command to list all the pods, which have long, complicated names, and then cut and paste the name into another Kubernetes command to list the log. Brochard wanted an easier workflow that didn’t involve leaving Emacs or using the mouse. His solution is Kubernetes mode.

What I really like about the talk is that he incrementally builds up his solution in much the same way that Kris Jenkins did when building his Spotify client. He starts in the shell showing us how to call Kubernetes to list the pods and get the logs, then he writes a bit of Emacs code to call the code and put the results in a buffer using shell-command-to-string, and then split-string to get a (Lisp) list of pods.

Next he wants a way of picking the correct pod. I probably would have used ivy-completing-read or one of its brethren but Brochard chose to use tabulated-list-mode, which makes sense for him because he is going to derive Kubernetes mode from tabulated-list-mode. By combining this with his previous code to get the list of pods, he has a naive version of the desired workflow. The problem is that large files can hang Emacs while they’re being collected and, of course, trying to tail and follow the log will lock everything up. He solves that by substituting call-process and start-process (depending on whether he wants to tail or not) for shell-command-to-string. Now he a workflow with good performance and that supports tailing the log.

Finally, he uses transient from Magit to wrap his mode up in a nice UX. Again, he does this incrementally so you can follow each step.

All his intermediate code is in code blocks in an Org file so it’s easy to follow along or go back afterwards to clarify something you missed. He also has a link to the full implementation of the mode if you want to see everything together or try it out. As I said, this is a really good talk and definitely worth watching. It’s about 33 and half minutes long so plan accordingly.

This entry was posted in General and tagged . Bookmark the permalink.