unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* where-is-internal question
@ 2005-09-07  0:54 Drew Adams
  2005-09-07  3:19 ` Drew Adams
  2005-09-07  8:28 ` Kim F. Storm
  0 siblings, 2 replies; 15+ messages in thread
From: Drew Adams @ 2005-09-07  0:54 UTC (permalink / raw)


I haven't looked into this in detail - forgive my incomplete understanding.
I'm looking for help on where-is-internal - as regards command remapping (I
guess).

In previous Emacs versions, I could do this, to bind stuff that is bound to
self-insert-command in the global map:

 (dolist (key (where-is-internal 'self-insert-command global-map))
   (define-key my-map key 'my-command))

Now, however, it looks like I need to do something like the following. Let
me know if I'm missing something, and there is a simpler way.

(dolist (key (or (condition-case nil
                     (where-is-internal 'self-insert-command global-map nil
nil t)
                   (wrong-number-of-arguments nil))
                 (where-is-internal 'self-insert-command global-map))) ; use
old version
  (define-key my-map key 'my-command))

IIUC (probably not), the 5th arg to where-is-internal is needed here,
because of some remapping done to self-insert-command (?). The NEWS file
says this, for Emacs 22.1:

  `where-is-internal' now returns nil for a remapped command (e.g.
  `kill-line', when `my-mode' is enabled), and the actual key binding for
  the command it is remapped to (e.g. C-k for my-kill-line).
  It also has a new optional fifth argument, NO-REMAP, which inhibits
  remapping if non-nil (e.g. it returns "C-k" for `kill-line', and
  "<kill-line>" for `my-kill-line')

I guess that explains what's going on here, but I don't quite follow it. I
also searched NEWS for info on self-insert, but I didn't see anything that I
understood as being related. All I know is that if I don't use a non-nil 5th
argument, I don't get the bindings I'm after; if I do use it, that works.

I'm guessing that self-insert-command must be remapped, and that is why I
need to use a non-nil 5th arg - to prevent where-is-internal from returning
nil for each of the (remapped) self-insert-command bindings.

Could someone please clear up for me just what is going on here?

In ignorance, I'm thinking that the new 5th arg should be defined the other
way 'round: nil should do what t does now, to give better backward
compatibility. That way (I think), I would be able to do just
(where-is-internal 'self-insert-command global-map) in all Emacs versions.

Please let me know what I'm missing (>= 80%, I'm sure). Thanks.

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

* RE: where-is-internal question
  2005-09-07  0:54 where-is-internal question Drew Adams
@ 2005-09-07  3:19 ` Drew Adams
  2005-09-07  8:37   ` Kim F. Storm
  2005-09-07  8:28 ` Kim F. Storm
  1 sibling, 1 reply; 15+ messages in thread
From: Drew Adams @ 2005-09-07  3:19 UTC (permalink / raw)


    In previous Emacs versions, I could do this, to bind stuff that
    is bound to self-insert-command in the global map:

     (dolist (key (where-is-internal 'self-insert-command global-map))
       (define-key my-map key 'my-command))

    Now, however, it looks like I need to do something like the
    following.
    (dolist (key (or (condition-case nil
                         (where-is-internal 'self-insert-command
                                            global-map nil nil t)
                       (wrong-number-of-arguments nil))
                     (where-is-internal 'self-insert-command
                                        global-map)))
      (define-key my-map key 'my-command))

I forgot to mention that, whereas the above code is lightning-quick in Emacs
20 (without the 5th arg), in Emacs 22 it takes about 5 _seconds_ (on a
pretty fast machine). Why so slow?

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

* Re: where-is-internal question
  2005-09-07  0:54 where-is-internal question Drew Adams
  2005-09-07  3:19 ` Drew Adams
@ 2005-09-07  8:28 ` Kim F. Storm
  2005-09-07 12:12   ` Stefan Monnier
  2005-09-07 13:52   ` Drew Adams
  1 sibling, 2 replies; 15+ messages in thread
From: Kim F. Storm @ 2005-09-07  8:28 UTC (permalink / raw)
  Cc: Emacs-Devel

"Drew Adams" <drew.adams@oracle.com> writes:

> I haven't looked into this in detail - forgive my incomplete understanding.
> I'm looking for help on where-is-internal - as regards command remapping (I
> guess).
>
> In previous Emacs versions, I could do this, to bind stuff that is bound to
> self-insert-command in the global map:
>
>  (dolist (key (where-is-internal 'self-insert-command global-map))
>    (define-key my-map key 'my-command))

What's wrong with

       (if (boundp 'this-original-command)
            (define-key my-map [remap self-insert-command] 'my-command)
          (dolist (key (where-is-internal 'self-insert-command global-map))
            (define-key my-map key 'my-command)))

Since you are (probably) going to use my-map as a local map, it will
be searched before other maps which may also have remapped
self-insert-command so your mapping will take precedence.

>
> Now, however, it looks like I need to do something like the following. Let
> me know if I'm missing something, and there is a simpler way.
>
> (dolist (key (or (condition-case nil
>                      (where-is-internal 'self-insert-command global-map nil
> nil t)
>                    (wrong-number-of-arguments nil))
>                  (where-is-internal 'self-insert-command global-map))) ; use
> old version
>   (define-key my-map key 'my-command))
>
> IIUC (probably not), the 5th arg to where-is-internal is needed here,
> because of some remapping done to self-insert-command (?). The NEWS file
> says this, for Emacs 22.1:
>
>   `where-is-internal' now returns nil for a remapped command (e.g.
>   `kill-line', when `my-mode' is enabled), and the actual key binding for
>   the command it is remapped to (e.g. C-k for my-kill-line).

Since kill-line is remapped to some other command, kill-line is not
on any keys, and so where-is-internal (in its default form) should
return nil (IMO).

> In ignorance, I'm thinking that the new 5th arg should be defined the other
> way 'round: nil should do what t does now, to give better backward
> compatibility. That way (I think), I would be able to do just
> (where-is-internal 'self-insert-command global-map) in all Emacs versions.

I don't mind such a change (where-is-internal is a low-level function
after all), but I strongly doubt you should be using it like you do in
the first place -- although I cannot tell you what to use instead :-).

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

* Re: where-is-internal question
  2005-09-07  3:19 ` Drew Adams
@ 2005-09-07  8:37   ` Kim F. Storm
  2005-09-08  2:41     ` Richard M. Stallman
  0 siblings, 1 reply; 15+ messages in thread
From: Kim F. Storm @ 2005-09-07  8:37 UTC (permalink / raw)
  Cc: Emacs-Devel

"Drew Adams" <drew.adams@oracle.com> writes:

>     In previous Emacs versions, I could do this, to bind stuff that
>     is bound to self-insert-command in the global map:
>
>      (dolist (key (where-is-internal 'self-insert-command global-map))
>        (define-key my-map key 'my-command))
>
>     Now, however, it looks like I need to do something like the
>     following.
>     (dolist (key (or (condition-case nil
>                          (where-is-internal 'self-insert-command
>                                             global-map nil nil t)
>                        (wrong-number-of-arguments nil))
>                      (where-is-internal 'self-insert-command
>                                         global-map)))
>       (define-key my-map key 'my-command))
>
> I forgot to mention that, whereas the above code is lightning-quick in Emacs
> 20 (without the 5th arg), in Emacs 22 it takes about 5 _seconds_ (on a
> pretty fast machine). Why so slow?

IIRC, the problem probably is that with command remapping,
where-is-internal in 22.x has to lookup the key binding for each
command to see if it remapped -- in the normal case (where a command
only has a few bindings), the penalty is neglible, but for
self-insert-command specifically, that will be time consuming.

The problem is that where-is-internal must return proper bindings for
commands that are remapped to, so there really don't see to be an easy
way to speed things up.

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

* Re: where-is-internal question
  2005-09-07  8:28 ` Kim F. Storm
@ 2005-09-07 12:12   ` Stefan Monnier
  2005-09-07 13:05     ` Kim F. Storm
  2005-09-07 13:52   ` Drew Adams
  1 sibling, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2005-09-07 12:12 UTC (permalink / raw)
  Cc: Drew Adams, Emacs-Devel

>        (if (boundp 'this-original-command)

Hmm... didn't know about that one.  I've used (fboundp 'command-remapping)
instead, which feels better to me.


        Stefan

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

* Re: where-is-internal question
  2005-09-07 12:12   ` Stefan Monnier
@ 2005-09-07 13:05     ` Kim F. Storm
  0 siblings, 0 replies; 15+ messages in thread
From: Kim F. Storm @ 2005-09-07 13:05 UTC (permalink / raw)
  Cc: Drew Adams, Emacs-Devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>        (if (boundp 'this-original-command)
>
> Hmm... didn't know about that one.  I've used (fboundp 'command-remapping)
> instead, which feels better to me.

Me too!

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

* RE: where-is-internal question
  2005-09-07  8:28 ` Kim F. Storm
  2005-09-07 12:12   ` Stefan Monnier
@ 2005-09-07 13:52   ` Drew Adams
  2005-09-07 15:11     ` Stefan Monnier
  2005-09-07 20:04     ` Drew Adams
  1 sibling, 2 replies; 15+ messages in thread
From: Drew Adams @ 2005-09-07 13:52 UTC (permalink / raw)


           (if (boundp 'this-original-command)
                (define-key my-map [remap self-insert-command] 'my-command)
              (dolist (key (where-is-internal 'self-insert-command 
                                              global-map))
                (define-key my-map key 'my-command)))
    
    Since you are (probably) going to use my-map as a local map, it will
    be searched before other maps which may also have remapped
    self-insert-command so your mapping will take precedence.

Thanks. I'll give it a try when I get a chance.    

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

* Re: where-is-internal question
  2005-09-07 13:52   ` Drew Adams
@ 2005-09-07 15:11     ` Stefan Monnier
  2005-09-07 16:03       ` Drew Adams
  2005-09-07 20:04     ` Drew Adams
  1 sibling, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2005-09-07 15:11 UTC (permalink / raw)
  Cc: Emacs-Devel

>            (if (boundp 'this-original-command)
>                 (define-key my-map [remap self-insert-command] 'my-command)
>               (dolist (key (where-is-internal 'self-insert-command 
>                                               global-map))
>                 (define-key my-map key 'my-command)))
    
Also the above dolist is advantageously replaced by a call to
substitute-key-definition (whose docstring mentions the use of the [remap
<foo>] binding).


        Stefan

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

* RE: where-is-internal question
  2005-09-07 15:11     ` Stefan Monnier
@ 2005-09-07 16:03       ` Drew Adams
  0 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2005-09-07 16:03 UTC (permalink / raw)


    >               (dolist (key (where-is-internal 'self-insert-command
    >                                               global-map))
    >                 (define-key my-map key 'my-command)))

    Also the above dolist is advantageously replaced by a call to
    substitute-key-definition

Of course! I've used substitute-key-definition a lot, but I forgot about its
optional OLDMAP arg. Thanks.

BTW, I wonder about the decision to not mention the PREFIX arg in the doc
string of substitute-key-definition. It is visible anyway, when you do C-h
f. I think it would be clearer to explicitly mention it, saying that it is
for internal use only, or that it is for recursive calls only. That is,
describe it, but tell people not to use it.

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

* RE: where-is-internal question
  2005-09-07 13:52   ` Drew Adams
  2005-09-07 15:11     ` Stefan Monnier
@ 2005-09-07 20:04     ` Drew Adams
  2005-09-07 20:25       ` Stefan Monnier
  1 sibling, 1 reply; 15+ messages in thread
From: Drew Adams @ 2005-09-07 20:04 UTC (permalink / raw)


    (define-key some-map [remap self-insert-command] 'my-command)

This really should be for help-gnu-emacs, but, since you've already been so
helpful...

What is a good way to "undo" this remapping? Looking at the doc (info), it
looks as if I cannot then do this:

    (define-key some-map [remap my-command] 'self-insert-command)

(The doc says that "remapping only works through a single level" and that
you can only remap an "ordinary binding".)

IOW, if I have a mode (e.g. minor mode) that does the remapping, what's a
good way to put things back the way they were in `some-map' (assuming that
map exists independently of the mode)?

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

* Re: where-is-internal question
  2005-09-07 20:04     ` Drew Adams
@ 2005-09-07 20:25       ` Stefan Monnier
  2005-09-08  1:59         ` Drew Adams
  2005-09-08  9:04         ` Richard M. Stallman
  0 siblings, 2 replies; 15+ messages in thread
From: Stefan Monnier @ 2005-09-07 20:25 UTC (permalink / raw)
  Cc: Emacs-Devel

>     (define-key some-map [remap self-insert-command] 'my-command)

> What is a good way to "undo" this remapping? Looking at the doc (info), it
> looks as if I cannot then do this:

  (define-key some-map [remap self-insert-command] nil)


-- Stefan

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

* RE: where-is-internal question
  2005-09-07 20:25       ` Stefan Monnier
@ 2005-09-08  1:59         ` Drew Adams
  2005-09-08  9:04         ` Richard M. Stallman
  1 sibling, 0 replies; 15+ messages in thread
From: Drew Adams @ 2005-09-08  1:59 UTC (permalink / raw)


    >     (define-key some-map [remap self-insert-command] 'my-command)
    > What is a good way to "undo" this remapping? 
 (define-key some-map [remap self-insert-command] nil)
    
Thanks. 

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

* Re: where-is-internal question
  2005-09-07  8:37   ` Kim F. Storm
@ 2005-09-08  2:41     ` Richard M. Stallman
  2005-09-08 10:14       ` Kim F. Storm
  0 siblings, 1 reply; 15+ messages in thread
From: Richard M. Stallman @ 2005-09-08  2:41 UTC (permalink / raw)
  Cc: drew.adams, emacs-devel

    IIRC, the problem probably is that with command remapping,
    where-is-internal in 22.x has to lookup the key binding for each
    command to see if it remapped -- in the normal case (where a command
    only has a few bindings), the penalty is neglible, but for
    self-insert-command specifically, that will be time consuming.

Could you show me where the code is that does this?
Maybe I can speed it up with some sort of memoization.

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

* Re: where-is-internal question
  2005-09-07 20:25       ` Stefan Monnier
  2005-09-08  1:59         ` Drew Adams
@ 2005-09-08  9:04         ` Richard M. Stallman
  1 sibling, 0 replies; 15+ messages in thread
From: Richard M. Stallman @ 2005-09-08  9:04 UTC (permalink / raw)
  Cc: drew.adams, emacs-devel

    > What is a good way to "undo" this remapping? Looking at the doc (info), it
    > looks as if I cannot then do this:

      (define-key some-map [remap self-insert-command] nil)

If this isn't obvious, let's add it to the Lisp Manual.

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

* Re: where-is-internal question
  2005-09-08  2:41     ` Richard M. Stallman
@ 2005-09-08 10:14       ` Kim F. Storm
  0 siblings, 0 replies; 15+ messages in thread
From: Kim F. Storm @ 2005-09-08 10:14 UTC (permalink / raw)
  Cc: drew.adams, emacs-devel

"Richard M. Stallman" <rms@gnu.org> writes:

>     IIRC, the problem probably is that with command remapping,
>     where-is-internal in 22.x has to lookup the key binding for each
>     command to see if it remapped -- in the normal case (where a command
>     only has a few bindings), the penalty is neglible, but for
>     self-insert-command specifically, that will be time consuming.
>
> Could you show me where the code is that does this?
> Maybe I can speed it up with some sort of memoization.

I really don't think this is necessary.

IMO lisp code should never use it like Drew's code did; with 22.x,
there are much more efficient ways to accomplish what he was doing.

Besides, I doubt you can find an efficient way to do memoization
for this case -- without making it even slower.

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

end of thread, other threads:[~2005-09-08 10:14 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-07  0:54 where-is-internal question Drew Adams
2005-09-07  3:19 ` Drew Adams
2005-09-07  8:37   ` Kim F. Storm
2005-09-08  2:41     ` Richard M. Stallman
2005-09-08 10:14       ` Kim F. Storm
2005-09-07  8:28 ` Kim F. Storm
2005-09-07 12:12   ` Stefan Monnier
2005-09-07 13:05     ` Kim F. Storm
2005-09-07 13:52   ` Drew Adams
2005-09-07 15:11     ` Stefan Monnier
2005-09-07 16:03       ` Drew Adams
2005-09-07 20:04     ` Drew Adams
2005-09-07 20:25       ` Stefan Monnier
2005-09-08  1:59         ` Drew Adams
2005-09-08  9:04         ` Richard M. Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).