1

Is there a way to hide a command from user?

Or, in other words, is there a way to undo an interactive spec?

Background: I have a derived mode, which is used in a special buffer as the buffer's mode. This buffer is generated by a command and the mode function is also called by this function. It does not make any sense for the user to run the derive mode function directly, and would in my case even lead to errors. Hence, in order to avoid confusion, I would like to remove the mode function from the list of commands.

Here a short example of my code's scheme:

(defun pseudo-sample-user-function ()
  "This is the only command designed for the user."
  (interactive)
  (create-new-buffer-and-switch-to-that-buffer)
  (pseudo-sample-mode))

(define-derived-mode pseudo-sample-mode nil "Pseudo Sample"
  "This should not be used by the user as command.")

If removing the mode from list of commands is not possible or not an good idea at all, would it then be possible to terminate mode activation? E.g. something like the following pseudo code:

(define-derived-mode pseudo-sample-mode nil "Pseudo Sample"
  "This should not be used by the user as command."
   (when pseudo-sample-mode-can-not-reasonably-be-activated
      (prevent-pseudo-sample-mode-activation)
      (user-error "Use pseudo-sample-user-function instead"))

EDIT: I came up with a solution to the second idea: prevent the user from using the command. I do it by utilising an around advice:

(defun pseudo-sample-mode-guard (&rest args)
  "Prevent user from using `pseudo-sample-mode' interactively."
  (when (interactive-p)
      (message "You should use `pseudo-sample-user-function'")))

(advice-add 'pseudo-sample-mode :before-until #'pseudo-sample-mode-guard)

However, this has the drawback of having to use an advice, which you should not do in code meant for other users...

1 Answer 1

3

The nearest language-supported way is:

(put 'my-command-to-suppress 'disabled 
     "Don't do it! No, really, don't! I mean it!")

This does not completely disable the command, but (by default) rather makes the user confirm that they do indeed want to run it.

You can make even that harder by overriding the default prompting mechanism with your own (presumably stricter) function assigned to variable disabled-command-function.

2
  • 1
    I like this, thanks.
    – theldoria
    Apr 27, 2022 at 13:53
  • 1
    You're very welcome! Apr 27, 2022 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.