Config File Languages

Joe Marshall over at Abstract Heresies has a short allegory on designing configuration file languages. We’ve probably all been there. It starts off simple: he just wants a configuration file that handles key/value pairs. But then he finds he needs a bit more flexibility—Marshall’s example is getting a value from an environment variable—so his key/value scheme added an escape sequence to deal with this case. In the way of entropy everywhere, it just goes on and on.

Pretty soon, he realizes that what’s really going on is that he’s designing a crappy configuration language on the fly rather than designing a good one from the outset. As I said, we’ve all been there.

His last paragraph sums up the point:

If we had some forethought, we would have realized that we were designing a language and we would have put the effort into making it a good one. If we’re lazy, we’d just pick an existing good language. Like Lisp.

That brings us to Emacs. The Emacs configuration file is written in the same language that most of the rest of Emacs is: Elisp. That means that the configuration can be as rich as desired, including even adding or rewriting entire functions. That’s the real secret to Emacs’s famous extensibility: You can add to the core body of code right in the configuration file.

Sadly, not every application has the advantage of being written in a Lisp dialect but you can still leverage the advantages of Lisp by using Lisp for the configuration file. A great example of this is Guile, a Scheme-based extension language that can be linked into applications written in another language.

With a little planning, you can avoid the descent into despair of Marshall’s apocryphal developer.

UPDATE [2023-07-28 Fri 15:25]: Added link to Marshall’s post.

Posted in General | Tagged , | Leave a comment

Eshell Configuration

Another video from Emacs Elements. This time it’s about Eshell configuration. I love Eshell and use it all the time. It’s mostly like a standard Unix shell but there are some surprises. The major surprise, of course, is that you can’t create pipelines the way you can in, say, Bash or Zshell. But there are some other gotchas. One of them is setting aliases.

It’s really easy to set an alias with Eshell—arguably easier than in any of the Unix shells—but it is different so it’s nice that the video discusses how to set and use them. Best of all, they’re stored in a plain text file that you can edit by hand if you’re inclined.

One of the other things the video mentions is some Elisp to provide a clear command for Eshell. You can type clear and it will blank the screen but the cursor is left in the wrong place and you can scroll backward to all the text you thought you were clearing. I combined that code with alias to make a real clear command. I modified the code a bit to

(defun eshell-clear-buffer ()
  "Clear the current Eshell buffer."
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)
    (eshell-send-input)))

and then set an alias to clear with

alias clear '(eshell-clear-buffer)

That solves a longstanding problem with my Eshell use so I was happy to discover it.

The video covers a few other subjects as well. You’ll learn how to set up Eshell to imitate Plan 9’s shell behavior, how to open files in Eshell, and where Eshell hides all your settings.

Finally, he strongly recommends Mickey’s article Mastering Eshell to learn everything you need to know about Eshell to use it effectively.

UPDATE [2023-08-14 Mon 16:15]: shell-send-inputeshell-send-input

Posted in General | Tagged | Leave a comment

Zamansky: Elisp 4

Mike Zamansky is back with the fourth video in his Learning Elisp series. In this video, Zamansky begins building some real functions by considering the rot13 utility. Rot13 is a simple function built into Emacs and other utilities mainly as a way of protecting those with delicate sensibilities from potentially upsetting material. The idea is that each letter of some text is rotated to the right by 13 letters with the appropriate wrap around. If you apply it again, you get the original text back.

It’s conceptually simple but there are a bunch of annoying details such as the fact that the lower case a is not 0 or 1 but 97. There are some other wrinkles as well that the video explores as Zamansky develops the material.

He begins with a lengthy detour into the history of Usenet and the genesis of rot13 as a way of sparing the faint of heart from possibly disturbing material. The text was “encrypted” with rot13 which served as a warning of dangerous material ahead. Those who felt up to it could “decrypt” the offensive material to see the joke or whatever it was.

In his preliminary version of rot13, Zamansky introduces the mapcar function as a way of operating on each character of a string and of anonymous functions as a way of providing a one off function that isn’t worth cluttering up the name space with.

The video is 34 minutes, 12 seconds but Zamansky provides links to skip over the Usenet history if you’re not interested. If you weren’t around in the days of Usenet, I recommend watching it to get a better idea of the context in which rot13 made sense.

Posted in General | Tagged | Leave a comment

Implementing Emacs Threads

Two or three weeks ago in the comments to my post How Many Years Have You Been Using Emacs? I opined that the biggest thing missing from Emacs was a decent threading model. I also admitted that it was a very hard problem and one that I was glad I didn’t have to solve.

Dickmao is a braver soul and decided to try a proof of concept. After all, he said, how hard could it be? The problem with implementing threads in Emacs is that historically there was no lexical binding; all the non-local symbols were dynamically bound globals.

Dickmao’s idea was simple: give each thread its own obarray so that it would have its own copy of each symbol. It didn’t take any time at all for things to blow up. He immediately encountered a static buffer in the print routine. He played wack-a-mole for a while but there always seemed to be another problem.

He explains all this in his video. After you watch it, you will have a much better understanding of the problem with threads in Emacs. The video is only 4 minutes, 12 seconds so it should be easy to fit in.

Posted in General | Tagged | Leave a comment

Emacs Elements on Emacs 29

Emacs 29 should be officially released soon. Many of you, I know, are already running it. For those that haven’t already taken the plunge, Emacs Elements has a series of three videos that cover the installation and some of the features of Emacs 29. The videos are from 3 months ago but somehow I missed them then.

The first video deals with compiling Emacs 29 from source. The steps are captured in a Bash script that works for Mint/Ubuntu and probably, with minimal adjustments, any other Linux distribution. It won’t work on Macs, of course, but the steps are pretty much the same and could be adapted without much work.

The build is pretty vanilla except for enabling SVG and native compilation with the aot (ahead of time) option. The aot options compiles the Elisp files to native code during the build process so that things are snappier when Emacs is run for the first time.

The second video is probably the most interesting for most of us. It takes a look at some of the new features in Emacs 29. I written about a number of posts and videos that talk about the Emacs 29 features [1, 2, 3, 4, 5] and it’s interesting how different the list of additions the commenters think are most important. Think of this video as one perspective on that question.

Finally, the last video discusses enhanced image features new to Emacs 29. I don’t deal with images much so I wasn’t too interested in this video but if you do work with images, it’s certainly worth a few minutes of your time.

The videos are all pretty short so you can probably fit one of them in whenever you have a few spare minutes.

Posted in General | Tagged | Leave a comment

Emacs 29.1 RC1

Good news from Eli Zaretskii: The first release candidate for Emacs 29.1 is available. This is, of course, the final stage of the prerelease cycle. Zaretskii says that if there are no problems with the release candidate, Emacs 29.1 will be released next week. That’s good news indeed.

I’m sure this release is stable and safe for even the most conservative Emacsers so if you can, help Zaretskii and the other developers with the final testing. As always, much thanks to all the developers for the huge amount of work that went into preparing this release.

Posted in General | Tagged | Leave a comment

Moving Text in Emacs

Charles Choi has another great Emacs post. It’s about Moving Text Elegantly in Emacs. The TL;DR is that Choi considers some elegant ways of moving various text objects forward and backwards. It’s really a generalization of the transpose-* functions. We all know about transpose-chars and transpose-words and some of us even remember that you can do the same with sentences. On my system, at least, transpose-sentences isn’t bound to any keys but I use it rarely enough that I don’t mind typing in the command. It’s easy to remember the name so I consider it part of my toolkit.

There’s also a transpose-sexps but it’s action is a bit more mysterious. When you’re in a Lisp buffer, it does just what you’d think it would but for other languages or text based buffers the situation is more complicated. In those cases the idea of an s-expression is replaced by the more general “balanced expression”. A balanced expression is something between matching delimiters like “”, [], <> and so on. Part of what makes things so complicated is that what those delimiters are depends on the buffer mode.

Choi’s idea is to use a combination of moving forward or backward over a text object and the transpose functions to create a series of functions to move those objects forward or backward over objects of the same type. He motivates this with the example

["a", "b", "c", "d"]

Suppose, he says, you want to move the "a" forward into the third position so that the list becomes

["b", "c", "a", "d"]

It’s easy to brute force that, of course, but if you have to do it repeatedly, it quickly becomes tedious. Choi creates a simple function using just forward-sexp and transpose-sexps that does this easily. He has several functions for moving various objects forward and backward.1

He also observes that these are seldom used routines and not worth remembering let alone binding a key sequence to. His solution is to add them to the Edit menu. That’s perfect because all you have to do is remember that they exist and can otherwise forget about them until you need them.

It’s a good post so take a look when you have a few minutes.

Footnotes:

1

If you want to experiment with the above example, be sure to do it in some sort of text buffer. In particular, it won’t work in the *scratch* buffer because that’s a Lisp buffer and its definition of “balanced expression” isn’t right for this example.

Posted in General | Tagged | Leave a comment

Red Meat Friday: Better Emacs Keybindings

OutOfCharm has a question. He wants to know how would you improve the Emacs keybindings if you could make any changes you wanted. The question is an honest one. He doesn’t like the current bindings and wants something he feels would be more coherent. But here’s the thing: the question doesn’t make any sense.

I can make any changes I want; you can make any changes you want; and OutOfCharm can make any changes he wants. If you don’t like a particular (or every) binding, it’s trivial to change it. It’s not just that the source code is available. It’s easier than that: you can simply use one of the key binding commands to set a command to whatever binding you like. And your changes are pretty much portable. They’ll work for any version of Emacs as long as it has the command the binding refers to exists.

I’m comfortable with the default bindings and don’t feel the need to change them but I do assign bindings to new commands. I try to make them consistent with existing bindings but anything I choose is just going to work as long as it doesn’t conflict with the binding for some other command that I need.

Questions like this tend to put my back up because they seem unnecessarily aggressive. They always have the vibe—and I’m sure this was not OutOfCharm’s intent—of “Why did you rubes choose such stupid bindings? Here’s what I would have chosen if I were in charge.” The thing is, you are in charge. You can choose whatever bindings you like but the current bindings have been in place for about 40 years so they may have something to recommend them even if that something isn’t obvious to you.

Posted in General | Tagged , | Leave a comment

Emacs Elements on Avy

Emacs Elements has a great video on one of my favorite navigation packages: Avy. The idea is a generalization of Steve Yegge’s recommendation to navigate with search. When I first read Yegge’s tip it was an Epiphany to me. Suddenly, moving around a buffer was quick and easy. Abo-abo’s Avy generalizes Yegge’s idea and makes it even faster and easier. It’s my goto way of navigating and I’ve already used it several times while writing this post.

The video is an excellent introduction to Avy and what it can do. As the video notes, there are several commands that are similar but differ in subtle ways to provide a wide range of functionality. My first choice when I need a navigation command is always avy-goto-word-1. It provides an easy way of jumping to a word by specifying its first character.

My second most used Avy command is avy-goto-char-timer. The video fails to capture its true utility. The point of the timer is that you can type any number of characters until there’s a delay of the timer’s value and Avy will identify all the strings beginning with those characters and jump to the start of the one you select. It’s almost like using incremental search as Yegge suggests but the command leaves the point at the beginning of the string, which is convenient.

The video demonstrates two commands that I wasn’t aware of: avy-copy-line and avy-move-line that do just what it says on the tin. When you invoke one of the commands, each line is added to the list of targets and the one you choose is copied or moved to the point.

Finally, there’s avy-goto-line that allows you to immediately jump to a line without knowing its absolute line number. I have it bound to Meta+g Meta+g so that it’s really easy to invoke.

If you’re interested, here’s my complete Avy configuration:

(use-package avy
  :ensure t
  :diminish avy-mode
  :bind (("H-a"     . avy-goto-word-1)
         ("H-s"     . avy-goto-char-timer)
         ("M-g M-g" . avy-goto-line)
         ("M-g g"   . avy-goto-line)
         ("H-A"     . avy-goto-char))
  :config (setq avy-all-windows nil))

The Hyper+A binding is for those rare occasions when I want to jump into the middle of a word. The problem with it is that it generates a large number of targets. I never use the Meta+g g binding and don’t know why it’s still there.

If you aren’t using Avy, you’re working too hard. Take a look at the video to see what it has to offer. The video is only 7 minutes, 29 seconds so it should be easy to fit in.

Posted in General | Tagged | Leave a comment

Balancing Windows

Here’s a tiny tip from Zachary Kanfer. Like me, he prefers to keep his Emacs windows the same size. That’s pretty easy to do: when you split or delete a window resulting in an unbalanced layout, you can merely call balance-windows with Ctrl+x +.

I tend to stay with two windows so this doesn’t come up often for me but Kanfer has a richer window environment and one day he realized that he was doing too much manual work keeping his windows balanced. He noticed that they became unbalanced when he deleted or split windows so he wrote 4 lines of Elisp that arranged for those functions to call balance-windows after they ran. Now he longer has to think about balancing his widows; it just happens.

Posted in General | Tagged | Leave a comment