emacs-devel
[Top][All Lists]
Advanced

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

Re: bizarre problem with minor mode defined using define-minor-mode


From: Stefan Monnier
Subject: Re: bizarre problem with minor mode defined using define-minor-mode
Date: Sun, 16 Jan 2011 01:46:44 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

>> I really can't understand who it could be effective.  My reading of the
>> code tells me that function binding is never used.

> (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



reply via email to

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