2024-08-12 How to require confirmation before launching an Emacs command

Some Emacs commands are potentially destructive (like kill-emacs, which exits Emacs) and may require some kind of confirmation. The usual way of confirming that this is really what I want to do is y-or-n-p (and a few more functions from its family) and yes-or-no-p. Sometimes, however, there is a command which does not invoke y-or-n-p or any similar function, but for some reason I want it to ask for confirmation. An example is mu4e-compose-reply (bound to R in Mu4e) – I usually want mu4e-compose-wide-reply (bound to W), aka “reply to all”.

I thought about advising mu4e-compose-reply, but then it occurred to me that Emacs already has a feature which ensures that I won’t invoke some command by accident: disabled commands. You can see how they work – just issue a command disabled by default. For example, assuming that you didn’t enable it, narrow-to-page is disabled, so pressing C-x n p shows what happens when you try to use a disabled command.

Now, it is enough to put (put 'mu4e-compose-reply 'disabled t) in your init.el. This piece of Elisp sets the property disabled of symbol mu4e-compose-reply to t. You can also define a message for the user instead of t:

(put 'mu4e-compose-reply 'disabled
     "It's better to use `mu4e-compose-wide-reply' instead.\n")

Interestingly, reading the manual I learned that there is a less invasive way to make sure that Emacs asks for confirmation when issuing some command:

(command-query 'mu4e-compose-reply "Do you really want to reply only to the sender?")

(You can also set the third argument to a non-nil value for Emacs to use yes-or-no-p instead of y-or-n-p). As usual, Emacs lets you do whatever you want, and in this case, it even lets you do it in an easy way (in fact, two easy ways!).

CategoryEnglish, CategoryBlog, CategoryEmacs