help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make function callable from some specific major-mode


From: Stefan Monnier
Subject: Re: Make function callable from some specific major-mode
Date: Wed, 19 Oct 2022 21:26:37 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Heime via Users list for the GNU Emacs text editor [2022-10-20 00:59:33] wrote:
> How can I make a function callable only whilst in some specific major-mode?

Usually: you don't (most of the time, it's a pointless effort: if the
users go through the trouble to call your function, then better presume
that they actually have a good reason to do so).

If that answer doesn't work, then the second answer: you don't, and
instead you test for a specific property on which your code depends (so
it will still be useful in a buffer using another mode, as long as your code
still makes sense there).

If you're still not satisfied because you really want to prevent users
from using your function even if it could be useful, then you test
(derived-mode-p '<THEMODE>) and signal an error if the test fails.

Another way is to add (declare (modes <THEMODES>)) to your function
definition: this won't prevent the function from being used elsewhere,
but it will tell `M-x` not to bother proposing this function if you're
not in one of the listed modes.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]