Zachary Kanfer

Feb 7, 2022

Make your first interface in Emacs.

I'm going to explain how to code your first interface in Emacs.

Many cool ideas require an interface. It allows for data to be displayed, and for more complicated inputs from the user. The user isn't forced to remember what actions they can do, because the interface can remind them.

Unfortunately, making an interface can be intimidating.

  • In most programs, you can't create interfaces at all.

  • Even software that allows you to create interfaces makes it very complicated. Why would Emacs be different?

  • Creating just a normal Emacs package can be intimidating.

Luckily, Emacs makes it easy. Let's begin.

Step 1: Start with a new major mode.

This major mode is the bedrock for all our functionality.

Use define-derived-mode to create your new major mode. Use C-h f define-derived-mode to see the arguments to that function. If you want to create a package called FOO, the first argument should be foo-mode, which names a function that enters your mode.

Step 2: Make a function to enter your mode.

This function will be called to go into your mode. Give it the same name as your mode.

Make it an interactive function so the user can call it by name. Inside it, switch to a buffer named specially for your package, delete everything in that buffer, and call the function we discussed in step 1 to set the major mode to your function's mode, e.g., foo-mode.

Step 3: What would you type to insert the interface? Convert that into code.

Imagine what your interface should look like, and type it out by hand. Then you can write code inside the function from step 2 to insert those same strings. Create some variables to store data, and then insert those variables in the interface when needed.

Step 4: Functionality goes in functions.

Any data manipulation that needs to happen should go through functions. So make some! They can be bound to keys if need be. Those functions should also reload the interface, so call the function from step 2 to redisplay the interface in the buffer.

That's all we need to do to get a basic interface working. You can keep extending your package to have more and more functionality, but you've already created a simple interface! Congratulations! This is the first step to making a useful Emacs package.

Zachary Kanfer

I write about programming, music, and comedy.