Site icon Random Thoughts

Command Discovery In Emacs

One thing that has annoyed me forever about Emacs is that when you define a command for a specific more, the commands become so… global.

I mean, you define a command in, say, eww-mode for navigating to the next link, and that command only works in that mode. But then you type `M-x ewwTAB’ to try to remember what the command to open an HTML file in eww is, and Emacs will spit back at you:

The vast majority of those commands make sense within eww only, and not in the *scratch* buffer where I was.

So now I’ve introduced markup for commands in Emacs that specify what modes they’re meant to be used it. So if you now type `M-x ewwTAB’ in *scratch*, Emacs will give you a distinctly shorter list:

Right! The command I was searching for was `M-x eww-open-file’.

It’ll take a long while until all commands in Emacs are tagged properly… but if you wanna start helping, please do! It’s not complicated, but it does require some thought, because commands that really should be global are commonly mixed in with mode-specific commands. Marking up eww.el took me about five minutes, and that’s a file with four different modes.

The basic idea is that commands, today, are defined as such:

(defun foo-thing (arg)
  (interactive "p")
  ...)

The `interactive’ thing says “this is a command”. This just needs to be annotated as to what mode it’s for:

(defun foo-thing (arg)
  (interactive "p" foo-mode)
  ...)

Simple! Now Emacs will know that `foo-thing’ is a command that’s only applicable for `foo-mode’ (or any modes that is derived from `foo-mode’).

The `foo-mode’ thing can also be a list of modes if the command applies to several modes, but that’s actually kinda rare.

To get started with Emacs development, have a peek at this blog post. Building Emacs is fun and easy.

Start fixing up modes, send in patches, and watch how `M-x TAB’ gets more and more usable!

Exit mobile version