all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Make function callable from some specific major-mode
Date: Wed, 19 Oct 2022 21:26:37 -0400	[thread overview]
Message-ID: <jwvpmen2sve.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: FH3aBfNi-bfVxkchw5BZ_p9iftvhkjXTlTgJiItZloYkT1TrM5-LsjhdZX8_TlSYxw8AjdXKI3sCTRdanxnZhCv_8uP2hCozSbj4x7G-EBc=@protonmail.com

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	other threads:[~2022-10-20  1:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-20  0:59 Make function callable from some specific major-mode Heime via Users list for the GNU Emacs text editor
2022-10-20  1:26 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvpmen2sve.fsf-monnier+emacs@gnu.org \
    --to=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.