Prot On Writing A Custom Mode Line

Protesilaos Stavrou (Prot) has a really interesting video on writing a custom mode line. If you’re like me, your mode line customizations are limited to adding the line and column indicators and using diminish to inhibit displaying various minor modes. You can, of course, do more but the details of making a custom mode line are surprisingly fussy.

Prot’s video shows us how to do it. He starts by showing us his custom mode line. It has some nice features. One thing I liked is that only the window with focus has the full mode line. The other windows have an abbreviated mode line showing only the buffer name and its major mode. That makes it easy to tell which window has focus so he doesn’t need to change the background as the default mode line does.

Prot has some idiosyncratic notions of what should be on the mode line—he doesn’t like the line and column indicators, for example—but that’s why, after all, you have custom mode lines. His video is not about how to reproduce his mode line; it’s about how to make your own.

As I said, the process can be a bit fussy and there are all kinds of gotchas but once you know a few rules it’s pretty easy to build your own. The nice thing is you can display your candidate mode line as you build it without having to restart Emacs.

As usual, Prot provides links to his own configuration so you can copy some or all of what he did. He also provides a copy of the intermediate code he used while developing his sample mode line so you don’t have worry about copying it down from the video. The runtime is 41 minutes 53 seconds so you’ll have to schedule some time but if you’ve ever wanted to customize your mode line, it will be well worth your time.

Posted in General | Tagged | Leave a comment

Emacs 29.1 Released

Eli Zaretskii writes to announce that Emacs 29.1 has been released and is available in the usual places. As usual, I compiled it from source so I don’t know if the prebuilt distributions have been released yet.

In any event, this post is being written with the new version so there are no obvious nasty surprises. As usual, many thanks to Zaretskii and everyone else who worked so hard to bring this new release to us. These guys are heroes who don’t get nearly enough recognition for what they do. If you run into any of them, be sure to buy them a beer.

Posted in General | Tagged | Leave a comment

Composing Avy

A couple of years ago Karthik Chikmagalur published a long, dense post on how Avy can do anything. For some reason—I no longer remember why—I never added his post to my blog queue. Recently his comment to my post Emacs Elements on Avy reacquainted me with his post and I’m astounded that I didn’t write about such a rich and useful post.

I’ve written many times about how Avy is one of my most-used commands. It makes it almost trivially easy to move around in the text on the screen. Avy has several commands—more than appear in the documentation. One of Karthik’s two main points is that there are too many commands to remember or bind so you should settle on just one and use it for everything. There’s some merit to that idea. He uses avy-goto-char-timer, which can, indeed, perform most of the tasks of the other commands. I use it everyday but my most used Avy command is avy-goto-word-1, which I like because it considers only letters beginning words and thus reduces the number of targets. As a practical matter, I restrict myself to those two Avy commands.

His second main point is that Avy is much more general and useful than you thought. After you select a target, the default action is to jump to that location but other actions are possible. There are several alternative actions, which you can see by typing ? when the list of targets appears.

But wait. There’s more. You can also easily add your own actions as Karthik demonstrates. His post shows several such actions with the code to achieve them. He also has a link to a file with all the code if you want to steal some or all of them.

This is a really good post and worth your time to read even though it is long. And, as I said in my Emacs Elements on Avy post, if you aren’t using Avy, you’re working too hard.

Posted in General | Tagged | Leave a comment

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