Prompting With The Interactive Command

Most Emacs users who are at all familiar with Elisp know that you have to specify the interactive command for functions if you want them to be interactive—that is, if you want them to be callable with Meta+x. Most of those people also know that you can use the interactive command to prompt a user for function parameters.

What is less well known is that you can also pass a list of parameters for the function in the interactive command. By itself, that’s not too useful, of course, but rather than specifying a list, you can specify a function that returns a list. The function can do anything as long as it returns a list. Arialdo Martini has a post that illustrates this nicely.

He looks at the problem of surrounding a region with a pair of delimiters. The question is how to get the two delimiters. The most obvious—but also most difficult—solution is to simply add code to the body of the function to prompt for the delimiters. It’s far easier to use the interactive command to get them and this also has the advantage of allowing the function to be called from Elisp without any prompting.

But what happens if you want to limit the choices to a predetermined set? That’s exactly what completing-read is for. Sadly, there’s no interactive parameter for this but you can write a function that uses completing-read and returns the delimiters as a list.

Martini’s post uses this idea to implement a set of functions that surrounds a region with any of a predetermined set of delimiters. As Martini says, there’s already plenty of functions and packages that do this. The point of his post is to explore how you can use the interactive command to pass parameters to a function. It’s a good post and worth a few minutes of your time to read.

Posted in General | Tagged | Leave a comment

A Simple Macro

I’ve just had another of my occasional workflow epiphanies where I suddenly realize that some tedious routine task I regularly perform could easily be automated. In this case, only partially automated but one step at a time.

Everyday, I take a measurement and record the result in a notebook1. Every other Sunday I copy the measurements from my notebook into a file. I’ve been maintaining that file since before I started using Emacs so it doesn’t follow the conventions that I use now. In particular, each entry has a date with the format mm/dd/yy.

It’s really a pain to enter those dates. I realized that I could easily use a macro to enter them. My idea was to put the dd value in a macro counter and let the macro increment it for me for each new entry. Here’s my new procedure:

  • I use Ctrl+x Ctrl+k Ctrl+f to set the counter format to %02d so that each dd entry will be exactly two digits.
  • I use Ctrl+x Ctrl+k Ctrl+c to set the counter to the day number of the first entry’s date.
  • I define the macro by pressing F3 mm/ Ctrl+x Ctrl+k Ctrl+i to enter the day number, and then finish the macro with /yy F4.

It seems like a small thing but it really makes entering the data easier. Of course, what I actually thought was that I should write a function to do the date automatically but dealing with dates is a pain and what’s really needed here is a closure so that subsequent dates can be returned. That’s possible and I may do it eventually, but in the meantime, the macro is a big improvement.

Afternote

After I wrote this, Marcin Borkowski wrote a nice post on using keyboard macros that reminded me that you can use F3 instead of Ctrl+x Ctrl+k Ctrl+i to insert the counter value. I use counters in keyboard macros so seldomly that I’d forgotten that.

Footnotes:

1

Don’t ask. It’s my sole remaining use of pencil & paper for record keeping.

Posted in General | Tagged | Leave a comment

Narrowing To Focus

Arthur A. Gleckler has a nifty post that discusses Emacs narrowing as a way to help with focus. The TL;DR is that sometimes you want to focus on a piece of code or other text and one way to do that is to temporarily remove all the surrounding text so that only the part you’re interested in is visible.

Emacs, of course, has you covered. It’s narrowing functions are just what’s called for. You can narrow to a region, a function, and various other parts of text. To a first approximation, it’s just what you need.

Gleckler, though, has an additional requirement. Sometimes he wants to look at the surrounding context and then return to the narrowed display. It’s easy, of course, to just widen the text, look at the context, and then narrow it again but that’s “complicated” enough that you can loose focus. He wanted a way to quickly pop in and out of narrowed displays.

He solved that problem with a bit of Elisp. The basic idea is that if there is an active region, he records the position of the region as an overlay with a special property and narrows to it. If no region is active, he looks for an overlay with the special property and uses its position as the inputs to narrow. Finally he binds a key shortcut to his function so that invoking it is as simple as it’s possible to be in Emacs.

The code is only a few lines and pretty easy to understand. If you’re interested in a similar facility or just want to see how he handled remembering which text to narrow to, take a look at his post. It’s short and easy to read.

Posted in General | Tagged | Leave a comment

BSD

Over at Abort Retry Fail there’s a very nice and comprehensive history and discussion of BSD Unix. I’m pretty familiar with most of this history but the article mentions some things that were new to me.

A good case can be made that it was BSD Unix that jump started the free software movement, although credit should be also given to AT&T or at least The Computing Science Research Center that embraced the idea as much as the AT&T lawyers would permit.

Because of their controlled monopoly status, AT&T couldn’t offer Unix commercially so they restricted distribution—at least initially—to academic institutions. Berkeley didn’t have those restrictions and were more interested in providing Unix to the world at large but couldn’t legally provide it to anyone who didn’t have an AT&T Unix source license.

This resulted in a mostly successful effort the remove all AT&T code from the Berkeley distribution. By the time of BSD 4.0 there were only a very few files that contained ATT&T IP. At this point the lawyers got involved and the famous UC Berkeley/ATT&T lawsuit was launched. In the end, BSD was essentially open sourced and we all benefited from distributions like NetBSD and FreeBSD. Eventually, of course, Linux became the standard “Unix” and, SCO notwithstanding, was completely free of prior IP.

If you’re at all interested in Unix history, this is a great article. It’s a bit long but there’s a lot of history to cover. It’s definitely worth your time.

Posted in General | Tagged | Leave a comment

Emacs Wow

Over at the Emacs subreddit, dehaticoder asks what things do you do in Emacs that make people say “Wow”. I’m probably an easy mark but I really love this type of post. They’re always enjoyable and sometimes I even learn something new. If you also enjoy these kinds of posts or want to learn some new techniques, take a look at dehaticoder’s post.

A lot of the answers are what you’d expect: Org mode, Magit, TRAMP, narrowing, and keyboard macros. Others had more specialized answers. One responder said that the thing that always elicited gasps of amazement from his coworkers was the speed at which he could make edits, and switch to other files or even machines, all without touching the mouse, of course.

My favorite was a variation on the grep/wgrep/make changes to grep buffer/save back to original files workflow that I first wrote about here. The simplest case is changing every occurrence of foo to bar in a set of files but the same technique can do more complicated tasks. Other suggestions were undo in region and undo tree that treats the undo history for the tree that it is.

There isn’t much ankle biting in the replies so it’s pleasant to read through them. As I said, you may even learn some new ideas for your own Emacs workflow.

Posted in General | Tagged | Leave a comment

Red Meat Friday: Avoid The Mouse

_nambiar has a short video on how painful it is to watch people use other editors and reach for the mouse every time they want to do some navigation or select text. He’s an Emacs user but his message will resonate with Vim users as well.

The video is definitely propaganda and certainly a fit subject for Red Meat Friday but no matter how much the naysayers try to deny it, it contains a nugget of truth. Some of the naysayers on the original reddit post make the ludicrous argument that his argument fails because some of his key bindings are long. Even leaving aside the fact that the first thing _nambiar says in the video is that you should learn to touch type, every Emacs user knows that you can rebind the bindings you use a lot to whatever you like.

There is, of course, a long standing argument about whether using the mouse or the keyboard for navigation is faster and some research claims that the mouse is faster but I’d be willing to bet that the editor used in those studies wasn’t Emacs. If you’re using some brain dead editor that makes you use the arrow keys to move around, then yes, the mouse may be faster but if you’re using an editor with robust keyboard navigation like Emacs or Vim, then I doubt a mouse could compete.

In any event, if you’re an Emacs or Vim user, enjoy the red meat.

Update [2024-03-15 Fri 15:39]: The “research” claims that the mouse not the keyboard is faster.

Posted in General | Tagged , | Leave a comment

More On The Emacs World Time Clock

As I’ve written before, Emacs has a world clock. Well, it’s not really a clock: just a list of times in various cities but it’s great for today’s remote work place. If your colleagues are stationed all over the world, it’s really useful to know what time it is where they are so you don’t try to contact them at 3 in the morning their time.

As the above linked post says, it’s possible to edit the list of cities listed. The method that Choi uses (in the above link) to configure them is the custom interface but others may prefer a more Lispy way of configuring it.

Bozhidar Batsov also addresses the world clock function and shows us how to configure it in, say, our init.el file. It’s easy to do except for knowing what the proper time zone is for the city you’re interested in. It’s easy to find that out by searching for “iana time zone <city>” For example, if I wanted to add Glasgow to the list, I would search for “iana time zone glasgow” and discover that the proper time zone is “Europe/London”. There’s also a database that you can download that contains all the supported places but it’s definitely overkill for this application.

Batsov also notes that you can edit the time string that gets displayed. Basically, you can specify any format supported by format-time-string. You might find that useful for further automation.

This is just another small example of how Emacs helps smooth your daily workflow. Even if you only occasionally want to know what time it is at Aunt Millie’s, it’s worthwhile looking up her time zone and adding it to the list.

Update [2024-03-15 Fri 12:41]: Glascow → Glasgow.

Posted in General | Tagged | Leave a comment

Org-linker

I’ve had this in my queue for a while but I wanted to try it out before I wrote about it. Sadly, I just haven’t had the time and probably won’t in the immediate future. Therefore, I’m writing about it now before it ages out.

This looks like a useful project for those of us who use org-attach and are worried about the problem of using the file name as the link. Org-linker uses a UUID instead and jin-cg claims that it’s more robust.

There are more details at the GitHub repository for the project so be sure to take a look at that if you’re interested in this project. If you’ve used it, drop a note and let us know how you like it.

Posted in General | Tagged , | Leave a comment

Boeing Continues To Founder

Boeing was a great company. For many years it defined the aviation industry by designing and building the best aircraft possible. The company thrived in large part because its culture was informed by an engineering ethos. Even their top executives had an aviation engineering background.

Then disaster struck. The government—in its omniscience—decided that Boeing should merge with McDonald Douglas and pressured Boeing to do so. The result was that the company was captured by suits with no engineering background or knowledge. That resulted, among other things, in a CEO who thought it made sense to hire and fire engineers according to short term needs.

The results have been aircraft crashes and door plugs blowing out in flight. We’re beginning to see the wages of these mishaps. People—not just the usual paranoids, but knowledgeable folks with aviation experience—are refusing to fly on the Boeing 737 Max. Take a look at this article that discusses the refusal of Ed Pierson, a man with military aviation experience and a ten year career at Boeing, to fly on the 737 Max. When he discovered, despite his explicit instructions to the contrary, that he was on a 737 Max, he demanded to get off the plane even though the hatch had been closed.

When informed travelers like Pierson refuse to fly on a particular aircraft, others are sure to follow and that is already happening as the article reports.

But that’s not all. NPR is reporting that Boeing is withholding key details about the recent door plug incident. NPR says that the NTSB has become increasing frustrated with its inability to get needed data from Boeing.

None of this bodes well for Boeing. If travelers refuse to fly on their aircraft, airlines will refuse to buy them. Perhaps it’s time to reconsider having the bean counters in charge.

Posted in General | Tagged | Leave a comment

Using Hook Functions

Arialdo Martini has a nice post on how to configure and use Emacs hook functions. Most Emacs users have a general idea about hook functions but Martini goes into more detail, especially as concerns their use in major modes. The TL;DR is that when a major mode starts, it calls a series of functions called “hook functions”.

That may seem a bit esoteric and something that only very advanced users would want to do but Martini gives some everyday uses that make sense for any Emacs user. His post is a reasonably detailed explanation of how to use hook functions and is well worth reading. He includes information how to handle conflicts where more than one one mode in a major mode hierarchy add the same function.

One thing that Martini covers that I didn’t know about is how to walk the major mode hierarchy to find out what modes the current mode is derived from. You probably won’t ever need to do this but it is interesting and points to how to investigate problems with conflicting hook functions.

The post is apparently Martini’s interpretation of some lessons he took from Protesilaos Stavrou and the usual Prot excellence shines through. As I say, it’s well worth taking a look at the post to get a better idea of what’s going on with hook functions.

Posted in General | Tagged | Leave a comment