How can I define an Elisp function such that it is available only under a specific mode?
(FWIW, at the moment, the mode I want to restrict a function to is comint
.)
You can't. But you can do either of the following:
When the function is invoked, it can test the current mode and do something appropriate. For example, for some modes, or all modes except some modes, it can be a no-op or it can raise an error.
If it is a command, you can ensure that it is not bound to a key in some modes, or in all modes except some modes.
Well, there is a way to make a function or variable essentially invisible in a given context, and that is to put its symbol in a different obarray from the default obarray. You can think of an obarray as a kind of a namespace.
If you are a relative novice then I wouldn't recommend this, but if this is really what you need then go for it. If you want an example of such use, you can take a look at library synonyms.el
(description).
Normally you simply avoid binding the command to any key maps except for the modes you want it to be used in. But that doesn't stop the user from using "m-x" to run the command by name.
The convention on function names for mode-specific commands is mode-whatever, such as dired-do-rename-file
. It is unlikely that the user will type that name, unless they really want it.
When defining your mode-only command with defun
, you can give a list of applicable modes to the interactive
declaration; this will prevent command name completion so that "m-x" won't see it elsewhere. This feature doesn't get used very often. For example, it is not used by dired
or org
modes, which are full of commands that don't work properly outside of buffers in their respective modes.
Your function can also try to test which mode(s) it is in, and do nothing unless it's the right mode. But the approach of figuring out the mode can be problematic, and you don't want to have to include such boilerplate at the beginning of all your functions. That's not the emacs way.