unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Looking up key myself
@ 2006-04-11  3:46 Herbert Euler
  2006-04-11  4:05 ` M Jared Finder
  0 siblings, 1 reply; 8+ messages in thread
From: Herbert Euler @ 2006-04-11  3:46 UTC (permalink / raw)


Hello,

When writing my first minor mode, I was told that if I want commands
in this minor mode able to call commands bound to the same key while
this minor mode is not activated, I should lookup key myself.  Following
(info "(elisp)Searching Keymaps"), I write

(defun xgp-casi2-lookup-key (key)
  (or (if overriding-terminal-local-map
          (lookup-key overriding-terminal-local-map key)
        (if overriding-local-map
            (lookup-key overriding-local-map key)
          (or
           (let ((map (get-text-property (point) 'keymap)))
             (if map
                 (lookup-key map key)))
           (let (val)
             (find t emulation-mode-map-alists
                   :test 'equal
                   :key '(lambda (alist)
                           (if (setq val (xgp-casi2-lookup-key-from-alist 
key alist))
                               t)))
             val)
           (xgp-casi2-lookup-key-from-alist key 
minor-mode-overriding-map-alist)
           (xgp-casi2-lookup-key-from-alist key minor-mode-map-alist)
           (let ((map (get-text-property (point) 'local-map)))
             (if map
                 (lookup-key map key)))
           (lookup-key (current-local-map) key))))
      (lookup-key (current-global-map) key)))

(defun xgp-casi2-lookup-key-from-alist (key alist)
  (let (val)
    (find t alist :test 'equal :key '(lambda (map)
                                       (setq map (cdr map))
                                       (if (setq val (lookup-key map key))
                                           t)))
    val))

Could somebody give me suggestions on this code?  Thanks in
advance.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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

* Re: Looking up key myself
  2006-04-11  3:46 Looking up key myself Herbert Euler
@ 2006-04-11  4:05 ` M Jared Finder
  2006-04-11  4:51   ` Herbert Euler
  2006-04-11 14:18   ` Stefan Monnier
  0 siblings, 2 replies; 8+ messages in thread
From: M Jared Finder @ 2006-04-11  4:05 UTC (permalink / raw)
  Cc: help-gnu-emacs

Herbert Euler wrote:
> Hello,
> 
> When writing my first minor mode, I was told that if I want commands
> in this minor mode able to call commands bound to the same key while
> this minor mode is not activated, I should lookup key myself.  Following
> (info "(elisp)Searching Keymaps"), I write

<snip>

I suggest using minor-mode-overriding-map-alist:

(let ((minor-mode-overriding-map-alist
        `((my-mode . ,(make-sparse-keymap)))))
   (key-binding "a"))

It's much simpler than rewriting key-binding.

   -- MJF

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

* Re: Looking up key myself
  2006-04-11  4:05 ` M Jared Finder
@ 2006-04-11  4:51   ` Herbert Euler
  2006-04-11  5:00     ` Miles Bader
  2006-04-11 14:18   ` Stefan Monnier
  1 sibling, 1 reply; 8+ messages in thread
From: Herbert Euler @ 2006-04-11  4:51 UTC (permalink / raw)
  Cc: help-gnu-emacs, emacs-devel

>From: M Jared Finder <jared@hpalace.com>
>To: help-gnu-emacs@gnu.org
>CC: emacs-devel@gnu.org
>Subject: Re: Looking up key myself
>Date: Mon, 10 Apr 2006 21:05:25 -0700
>
>I suggest using minor-mode-overriding-map-alist:
>
>(let ((minor-mode-overriding-map-alist
>        `((my-mode . ,(make-sparse-keymap)))))
>   (key-binding "a"))
>
>It's much simpler than rewriting key-binding.

But even in this way I need to find old key bindings, and
skip my minor mode in the finding process.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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

* Re: Looking up key myself
  2006-04-11  4:51   ` Herbert Euler
@ 2006-04-11  5:00     ` Miles Bader
  2006-04-11  5:10       ` Herbert Euler
                         ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Miles Bader @ 2006-04-11  5:00 UTC (permalink / raw)
  Cc: help-gnu-emacs, jared, emacs-devel

"Herbert Euler" <herberteuler@hotmail.com> writes:
>>(let ((minor-mode-overriding-map-alist
>>        `((my-mode . ,(make-sparse-keymap)))))
>>   (key-binding "a"))
>>
>>It's much simpler than rewriting key-binding.
>
> But even in this way I need to find old key bindings, and
> skip my minor mode in the finding process.

No, I think his point was that using this method, it _will_ ignore your
bindings during the lookup, because the key-map associated with `my-mode'
inside the let will be empty.

In other words, you (1) use a permanent entry in
`minor-mode-overriding-map-alist' with a non-empty keymap to define your
"normal" minor-mode bindings, and then (2) when you want to lookup the
underlying bindings, you temporarily bind `minor-mode-overriding-map-alist'
as shown above to hide your bindings during the key lookup.

[I've never tried this, so I dunno if it really works, but it looks very
elegant...]

-Miles
-- 
Occam's razor split hairs so well, I bought the whole argument!

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

* Re: Looking up key myself
  2006-04-11  5:00     ` Miles Bader
@ 2006-04-11  5:10       ` Herbert Euler
  2006-04-11 14:19       ` Stefan Monnier
       [not found]       ` <mailman.294.1144765276.9609.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Herbert Euler @ 2006-04-11  5:10 UTC (permalink / raw)
  Cc: help-gnu-emacs, emacs-devel

>From: Miles Bader <miles.bader@necel.com>
>Reply-To: Miles Bader <miles@gnu.org>
>To: "Herbert Euler" <herberteuler@hotmail.com>
>CC: help-gnu-emacs@gnu.org, jared@hpalace.com, emacs-devel@gnu.org
>Subject: Re: Looking up key myself
>Date: Tue, 11 Apr 2006 14:00:29 +0900
>
>No, I think his point was that using this method, it _will_ ignore your
>bindings during the lookup, because the key-map associated with `my-mode'
>inside the let will be empty.

Got it.  Great thanks to you two!

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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

* Re: Looking up key myself
  2006-04-11  4:05 ` M Jared Finder
  2006-04-11  4:51   ` Herbert Euler
@ 2006-04-11 14:18   ` Stefan Monnier
  1 sibling, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2006-04-11 14:18 UTC (permalink / raw)
  Cc: help-gnu-emacs, emacs-devel

>> When writing my first minor mode, I was told that if I want commands
>> in this minor mode able to call commands bound to the same key while
>> this minor mode is not activated, I should lookup key myself.  Following
>> (info "(elisp)Searching Keymaps"), I write

> <snip>

> I suggest using minor-mode-overriding-map-alist:

> (let ((minor-mode-overriding-map-alist
>        `((my-mode . ,(make-sparse-keymap)))))
>   (key-binding "a"))

You'll probably want to keep other entries on
minor-mode-overriding-map-alist, just in case:

   (let ((minor-mode-overriding-map-alist
          (cons `(my-mode . ,(make-sparse-keymap))
                minor-mode-overriding-map-alist)))
     (key-binding "a"))

> It's much simpler than rewriting key-binding.

Another way to do it is

  (let ((my-mode nil))
    (key-binding "a"))


-- Stefan

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

* Re: Looking up key myself
  2006-04-11  5:00     ` Miles Bader
  2006-04-11  5:10       ` Herbert Euler
@ 2006-04-11 14:19       ` Stefan Monnier
       [not found]       ` <mailman.294.1144765276.9609.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2006-04-11 14:19 UTC (permalink / raw)
  Cc: Herbert Euler, jared, help-gnu-emacs, emacs-devel

> In other words, you (1) use a permanent entry in
> `minor-mode-overriding-map-alist' with a non-empty keymap to define your
> "normal" minor-mode bindings, and then (2) when you want to lookup the
> underlying bindings, you temporarily bind `minor-mode-overriding-map-alist'
> as shown above to hide your bindings during the key lookup.

Point 2 is right, but point 1 isn't.  The permanent entry is on
minor-mode-map-alist, as usual.


        Stefan

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

* Re: Looking up key myself
       [not found]       ` <mailman.294.1144765276.9609.help-gnu-emacs@gnu.org>
@ 2006-04-11 21:24         ` Miles Bader
  0 siblings, 0 replies; 8+ messages in thread
From: Miles Bader @ 2006-04-11 21:24 UTC (permalink / raw)


Stefan Monnier <monnier@iro.umontreal.ca> writes:
> Point 2 is right, but point 1 isn't.  The permanent entry is on
> minor-mode-map-alist, as usual.

Thanks; I've never actually done any of this... :-)

-Miles
-- 
Love is a snowmobile racing across the tundra.  Suddenly it flips over,
pinning you underneath.  At night the ice weasels come.  --Nietzsche

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

end of thread, other threads:[~2006-04-11 21:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-11  3:46 Looking up key myself Herbert Euler
2006-04-11  4:05 ` M Jared Finder
2006-04-11  4:51   ` Herbert Euler
2006-04-11  5:00     ` Miles Bader
2006-04-11  5:10       ` Herbert Euler
2006-04-11 14:19       ` Stefan Monnier
     [not found]       ` <mailman.294.1144765276.9609.help-gnu-emacs@gnu.org>
2006-04-11 21:24         ` Miles Bader
2006-04-11 14:18   ` Stefan Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).