all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: help-gnu-emacs@gnu.org
Subject: Re: How to define a (derived) minor mode inaccessible to the user
Date: Fri, 22 Jan 2021 16:23:52 +0100	[thread overview]
Message-ID: <875z3pknav.fsf@mbork.pl> (raw)
In-Reply-To: <jwvr1mdukme.fsf-monnier+emacs@gnu.org>


On 2021-01-22, at 15:19, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> I'd like to define a special-purpose major mode, derived from
>> `special-mode', in such a way that the user cannot set it using M-x.
>
> Can you explain why?

Because I woulnd't like the user to accidentally turn it on?  (It might
be a bit of OCD on my part, too.)  Also, because I didn't know how to do
it and the very fact bugged me a bit;-).  Also, see below.

>> (It is to be called via another command.)
>
> That doesn't explain why it would be harmful to also expose the actual
> major mode command.

Maybe not harmful, but here's another argument: assume that I have this
new mode, let's call it "blue-mode", with all its commands named
"blue-...", and the entry point being the command "blue-display".  Now,
when the user says M-x blue- TAB, "blue-mode" will show up and pollute
the list (meaning: it will occupy some room on the list while being
useless, since blue-display is what user really wants to start using
blue-mode).

> E.g. You can do `M-x comint-mode` even though it likely won't do you
> much good since `comint-mode` needs to be used together with a process
> and is hence normally enabled by the command that launches the process.
>
>> It seems that `define-derived-mode' always sets the mode as
>> `interactive'.
>
> Indeed.
>
>> M-: (symbol-plist 'my--mode)
>> expecting an `interactive-form' entry on the plist
>
> The `interactive-form` property is virtually never used.  Instead, the
> interactive form is traditionally stored within the function.

So how does `execute-extended-command' know what to list, then?
A cursory look told me that it uses `read-extended-command', which uses
`commandp'.  So, how does `commandp' know?  (I'm not well versed in C,
but do I guess correctly that it checks the `interactive-form' property,
and if nil, it (somehow) looks for `(interactive ...)' in its
definition?)

> If you *really* care about hiding the major mode from the user
> I suggest you use an "obscure" or "scary" name.

Well, the convention is to use two dashes, but how can the end-user (not
knowing Elisp and the conventions) know that?

>> What's going on?  Is it possible to define a (derived) major mode so
>> that it can't be called interactively?
>
> Of course it is:
>
>     (define-derived-mode my-mode "My" "My doc.")
>     (defconst my-mode (symbol-function 'my-mode))
>     (fmakunbound 'my-mode)
>
> and then you can enable the mode with `(funcall my-mode)`.

So, basically you move the function definition from the function cell to
the value cell of `my-mode', right?  Clever!  Is it actually used
anywhere in Emacs?

Thanks,

--
Marcin Borkowski
http://mbork.pl



  reply	other threads:[~2021-01-22 15:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-22  5:11 How to define a (derived) minor mode inaccessible to the user Marcin Borkowski
2021-01-22  5:34 ` [External] : " Drew Adams
2021-01-22 15:01   ` Marcin Borkowski
2021-01-22 14:19 ` Stefan Monnier
2021-01-22 15:23   ` Marcin Borkowski [this message]
2021-01-22 15:38     ` Stefan Monnier
2021-01-28  7:42       ` Marcin Borkowski
2021-01-28 14:31         ` Stefan Monnier
2021-01-23 13:08 ` Michael Heerdegen
2021-01-28  7:45   ` Marcin Borkowski

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=875z3pknav.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --cc=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.