[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to define a (derived) minor mode inaccessible to the user
From: |
Marcin Borkowski |
Subject: |
Re: How to define a (derived) minor mode inaccessible to the user |
Date: |
Fri, 22 Jan 2021 16:23:52 +0100 |
User-agent: |
mu4e 1.1.0; emacs 28.0.50 |
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
Re: How to define a (derived) minor mode inaccessible to the user, Michael Heerdegen, 2021/01/23