Is there a way to hide a command from user?
Or, in other words, is there a way to undo an
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...