all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Attach a map to another
       [not found] <20220219130737.esdd2kou35avl75e.ref@Ergus>
@ 2022-02-19 13:07 ` Ergus
  2022-02-19 18:29   ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 7+ messages in thread
From: Ergus @ 2022-02-19 13:07 UTC (permalink / raw)
  To: help-gnu-emacs

Hi:

Recently I was making a minor-mode that apart from other stuff attach
some bindings to isearch-mode-map. I know that it is possible to add a
parent keymap and some inheritance.. and in the opposite direction I see
that it is possible to add bindings or prefix maps to another... But I
am not clear from the documentation if it is possible to add a keymap to
another without a prefix (with something like keymap-set or similar).

I think that having a keymap entry in another one makes the trick
according to the documentation:
(keymap
  (keymap
     (26 . extra-functions))
  (24 . normal-functions))

but I am wondering if there is an api to do that more conveniently?? At
we don't add bindings to keymaps with push so I don't expect to do so
with keymaps.

The idea is that in the mode it will be easier to do something like:

```
(defvar-keymap my-mode-isearch-map
     ...)

(define-minor-mode my-mode
    :keymap 'my-mode-keymap
    (if my-mode
        (keymap-set isearch-mode-map <something> my-mode-isearch-map)
      (keymap-unset isearch-mode-map <something>)))
```

Otherwise the isearch-mode-map keeps modified even after disabling the
mode.

I see that some packages around deal with this manually coping the
bindings, saving the previous map and replacing with an inherited one
iteratively and so on... but maybe there is something already there to
not reinvent the wheel?? And potentially more efficient??



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-19 13:07 ` Attach a map to another Ergus
@ 2022-02-19 18:29   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-02-19 19:24     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-02-20 11:07     ` Ergus
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-02-19 18:29 UTC (permalink / raw)
  To: help-gnu-emacs

> I think that having a keymap entry in another one makes the trick
> according to the documentation:
> (keymap
>  (keymap
>     (26 . extra-functions))
>  (24 . normal-functions))

Yes, it's the low-level representation used for
multi-keymap inheritance.

> but I am wondering if there is an api to do that more conveniently??

The only API available which uses that is `make-composed-keymap`.

[ Well, there's also `lookup-key` since multiple inheritance can appear
  naturally in some cases as the result of looking up bindings in
  keymaps with single inheritance.  Fixing those corner cases was
  actually the original motivation for introducing support for multiple
  inheritance.  ]

> (define-minor-mode my-mode
>    :keymap 'my-mode-keymap
>    (if my-mode
>        (keymap-set isearch-mode-map <something> my-mode-isearch-map)
>      (keymap-unset isearch-mode-map <something>)))

This code isn't right, but I see what you mean, and indeed, that's
a good use case.

We sadly don't yet have good functions for that, so you have to get
dirty and do it by hand, with something like:

    (define-minor-mode my-mode
      (if my-mode
          (cl-pushnew my-mode-map (cdr isearch-mode-map))
        (delq my-mode-map isearch-mode-map)))


-- Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-19 18:29   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-02-19 19:24     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-02-20 11:10       ` Ergus via Users list for the GNU Emacs text editor
  2022-02-20 11:07     ` Ergus
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-02-19 19:24 UTC (permalink / raw)
  To: help-gnu-emacs

> We sadly don't yet have good functions for that, so you have to get
> dirty and do it by hand, with something like:
>
>     (define-minor-mode my-mode
>       (if my-mode
>           (cl-pushnew my-mode-map (cdr isearch-mode-map))
>         (delq my-mode-map isearch-mode-map)))

BTW, this sample code is badly broken: the above `define-minor-mode`
will create a buffer-local minor mode whereas the modification we apply
to `isearch-mode-map` will be global :-(


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-19 18:29   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-02-19 19:24     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-02-20 11:07     ` Ergus
  2022-02-20 14:52       ` Stefan Monnier
  1 sibling, 1 reply; 7+ messages in thread
From: Ergus @ 2022-02-20 11:07 UTC (permalink / raw)
  To: Stefan Monnier,
	Stefan Monnier via Users list for the GNU Emacs text editor,
	help-gnu-emacs

Thanks Stefan... Good to know... Do you think it worth doing this a feature request?? I think that many packages will benefit from it... Because I have seen many of then dealing with this issue in different manners and not all of them succesfull.
Maybe the new keymap package may include something in keymap-set....


Best and thanks,
Ergus




On February 19, 2022 7:29:56 PM GMT+01:00, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>> I think that having a keymap entry in another one makes the trick
>> according to the documentation:
>> (keymap
>>  (keymap
>>     (26 . extra-functions))
>>  (24 . normal-functions))
>
>Yes, it's the low-level representation used for
>multi-keymap inheritance.
>
>> but I am wondering if there is an api to do that more conveniently??
>
>The only API available which uses that is `make-composed-keymap`.
>
>[ Well, there's also `lookup-key` since multiple inheritance can appear
>  naturally in some cases as the result of looking up bindings in
>  keymaps with single inheritance.  Fixing those corner cases was
>  actually the original motivation for introducing support for multiple
>  inheritance.  ]
>
>> (define-minor-mode my-mode
>>    :keymap 'my-mode-keymap
>>    (if my-mode
>>        (keymap-set isearch-mode-map <something> my-mode-isearch-map)
>>      (keymap-unset isearch-mode-map <something>)))
>
>This code isn't right, but I see what you mean, and indeed, that's
>a good use case.
>
>We sadly don't yet have good functions for that, so you have to get
>dirty and do it by hand, with something like:
>
>    (define-minor-mode my-mode
>      (if my-mode
>          (cl-pushnew my-mode-map (cdr isearch-mode-map))
>        (delq my-mode-map isearch-mode-map)))
>
>
>-- Stefan
>
>

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-19 19:24     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-02-20 11:10       ` Ergus via Users list for the GNU Emacs text editor
  2022-02-20 14:54         ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Ergus via Users list for the GNU Emacs text editor @ 2022-02-20 11:10 UTC (permalink / raw)
  To: Stefan Monnier,
	Stefan Monnier via Users list for the GNU Emacs text editor,
	help-gnu-emacs

It is a pseudo code I wrote quickly :p... Adding a :global t may be enough?

On February 19, 2022 8:24:28 PM GMT+01:00, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>> We sadly don't yet have good functions for that, so you have to get
>> dirty and do it by hand, with something like:
>>
>>     (define-minor-mode my-mode
>>       (if my-mode
>>           (cl-pushnew my-mode-map (cdr isearch-mode-map))
>>         (delq my-mode-map isearch-mode-map)))
>
>BTW, this sample code is badly broken: the above `define-minor-mode`
>will create a buffer-local minor mode whereas the modification we apply
>to `isearch-mode-map` will be global :-(
>
>
>        Stefan
>
>

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-20 11:07     ` Ergus
@ 2022-02-20 14:52       ` Stefan Monnier
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2022-02-20 14:52 UTC (permalink / raw)
  To: Ergus; +Cc: Stefan Monnier via Users list for the GNU Emacs text editor

> Thanks Stefan... Good to know... Do you think it worth doing this a feature
> request?? I think that many packages will benefit from it... Because I have
> seen many of then dealing with this issue in different manners and not all
> of them succesfull.
> Maybe the new keymap package may include something in keymap-set....

Sounds like a good idea, yes.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Attach a map to another
  2022-02-20 11:10       ` Ergus via Users list for the GNU Emacs text editor
@ 2022-02-20 14:54         ` Stefan Monnier
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2022-02-20 14:54 UTC (permalink / raw)
  To: Ergus; +Cc: Stefan Monnier via Users list for the GNU Emacs text editor

Ergus [2022-02-20 12:10:35] wrote:
> It is a pseudo code I wrote quickly :p... Adding a :global t may be enough?

If that's the behavior you want, yes.  But if you want it to be
buffer-local, then you'll have to `(setq-local isearch-mode-map ...)`
and you can't modify the keymap in place.  IOW you'll want to use
`make-composed-keymap` plus some other function to later "remove"
your keymap.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-20 14:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20220219130737.esdd2kou35avl75e.ref@Ergus>
2022-02-19 13:07 ` Attach a map to another Ergus
2022-02-19 18:29   ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-02-19 19:24     ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-02-20 11:10       ` Ergus via Users list for the GNU Emacs text editor
2022-02-20 14:54         ` Stefan Monnier
2022-02-20 11:07     ` Ergus
2022-02-20 14:52       ` Stefan Monnier

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.