(elisp)Format of Keymaps: "A symbol whose function definition is a
keymap is also a keymap."
minor-mode-map-alist entries of the form `(allout-mode .
allout-mode-map)' apparently tells the emacs key resolution facility
to look in the function value of the symbol in the cdr. that's why
the fset i do works.
>> in fact, removing the fset leads to the same problems with the
>> regular, defun'ed allout-mode that i have been seeing with the
>> byte-compiled define-minor-mode defined allout mode.
>
> That's what I would expect, since I think this fset is a no-op.
it's not a no-op! as i said before, removing the fset changed
behavior. as i describe above, there's an explanation.
>> the problem with switching to define-minor-mode is that
>> define-minor-mode apparently associates the mode name with the keymap
>> value, itself, on minor-mode-map-alist.
>
> That's because that's what you told it:
>
> :keymap <exp>
>
> says to use the value of <exp> as the keymap, so ":keymap
> allout-mode-map" says to use the value of the variable as the keymap.
> If you want to use a symbol, then you need to quote it.
alas, that's apparently not so:
(defvar example-mode-map (make-sparse-keymap))
=> example-mode-map
(define-key example-mode-map "\C-cv" 'emacs-version)
=> emacs-version
(assq 'example-mode minor-mode-map-alist)
=> nil
(define-minor-mode example-mode
"example-mode docstring."
:keymap 'example-mode-map
nil)
=> (keymap (3 keymap (118 . emacs-version)))
(assq 'example-mode minor-mode-map-alist)
=> (example-mode keymap (3 keymap (118 . emacs-version)))
(it's helpful to discover that just executing the define-minor-mode
establishes the keymap in minor-mode-map-alist. i didn't expect that.
actually activating the mode doesn't change the situation.)
> This said, you can modify a keymap after the fact: as long as you don't
> do a (setq allout-mode-map <newmap>), you can modify allout-mode-map on
> the fly and those changes will take effect immediately without having to
> use an indirection through the allout-mode-map symbol.
are you suggesting doing a rplacd on the minor-mode-map-alist cell? i
guess that's better than the fset approach, since for the latter, in
addition to doing the obscure fset, i also have to do surgery to
remove the keymap that define-minor-mode puts on minor-mode-map-alist
(whether i provide :keymap with a keymap or a symbol).
> Stefan
> (elisp)Format of Keymaps: "A symbol whose function definition is a
> keymap is also a keymap."
> minor-mode-map-alist entries of the form `(allout-mode .
> allout-mode-map)' apparently tells the emacs key resolution facility
> to look in the function value of the symbol in the cdr. that's why
> the fset i do works.
I'm talking about my reading of the allout.el code: nowhere does your
code use the function cell of that variable, AFAICT. I know you
*intend* to use it, and I see you set it, but I don't see where it's
used (i.e. where you use the symbol as a keymap).
>>> in fact, removing the fset leads to the same problems with the
>>> regular, defun'ed allout-mode that i have been seeing with the
>>> byte-compiled define-minor-mode defined allout mode.
>> That's what I would expect, since I think this fset is a no-op.
> it's not a no-op! as i said before, removing the fset changed
> behavior. as i describe above, there's an explanation.
Then, I'm lost.
>>> the problem with switching to define-minor-mode is that
>>> define-minor-mode apparently associates the mode name with the keymap
>>> value, itself, on minor-mode-map-alist.
>>
>> That's because that's what you told it:
>>
>> :keymap <exp>
>>
>> says to use the value of <exp> as the keymap, so ":keymap
>> allout-mode-map" says to use the value of the variable as the keymap.
>> If you want to use a symbol, then you need to quote it.
> alas, that's apparently not so:
Indeed, I misremembered: the :keymap argument is not an expression, it's
a funny thing, described in the docstring as:
Optional KEYMAP is the default keymap bound to the mode keymap.
If non-nil, it should be a variable name (whose value is a keymap),
a keymap, or a list of arguments for `easy-mmode-define-keymap'.
If KEYMAP is a keymap or list, this also defines the variable MODE-map.
And it's not clear at all when/if that thing is evaluated (i.e. if it
can be an expression).
So if you use `allout-mode-map', it will use the value of that variable,
and if you use something else, it will turn it into a keymap, set it as
default value of allout-mode-map, and then use the value of
allout-mode-map. Since the allout-mode-map is already defined before,
this ends up having no effect. Here's a relevant sample of IELM
session:
ELISP> (macroexpand '(define-minor-mode allout-mode "doc" :keymap 'blabla))
(defvar allout-mode-map
(let ((m #1='blabla))
(cond ((keymapp m) m) ((listp m) (easy-mmode-define-keymap m))
(t (error "Invalid keymap %S" #1#))))
"Keymap for `allout-mode'.")
(with-no-warnings (add-minor-mode 'allout-mode 'nil allout-mode-map nil nil)))
I suggest to stay away from the :keymap argument. Instead, do it this way:
(defvar allout-mode--map <blabla>)
(defalias 'allout-mode-map allout-mode--map)
(defvar allout-mode-map 'allout-mode-map)
>> This said, you can modify a keymap after the fact: as long as you don't
>> do a (setq allout-mode-map <newmap>), you can modify allout-mode-map on
>> the fly and those changes will take effect immediately without having to
>> use an indirection through the allout-mode-map symbol.
> are you suggesting doing a rplacd on the minor-mode-map-alist cell?
No, I'm really talking about modifying the keymap itself directly rather
than modifying various variables that point to it. E.g. I'm suggesting
doing a bunch of define-key only. But if some of your changes involve
adding/moving bindings it's more difficult, so you could instead use
(setcdr allout-mode-map (cdr newmap)), but that would be ugly.
But the above
(defvar allout-mode-map 'allout-mode-map)
should work much more cleanly.
Stefan
> I'm talking about my reading of the allout.el code: nowhere does your
> code use the function cell of that variable, AFAICT.
Here:
(setq minor-mode-map-alist
(cons '(allout-mode . allout-mode-map)
minor-mode-map-alist)))
(allout is the only mode doing it that way.)
Andreas.
--
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."