all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
To: Brian Malehorn <bmalehorn@gmail.com>
Cc: 13948@debbugs.gnu.org
Subject: bug#13948: no key-binding-locus
Date: Mon, 02 Jun 2014 12:15:34 +0200	[thread overview]
Message-ID: <87k38zipmh.fsf@geodiff-mac3.ulb.ac.be> (raw)
In-Reply-To: <CAJB88a1Vg2uhQL9fDhLmzRU7-CDCCCbOHAApGRtid3RkxLq7tQ@mail.gmail.com> (Brian Malehorn's message of "Wed, 13 Mar 2013 16:34:48 -0400")

Brian Malehorn <bmalehorn@gmail.com> writes:
> Why isn't there a key equivalent to variable-binding-locus? As
> in, a way to figure out where a particular keybinding is coming
> from. For example,

> So implementing key-binding-locus would only be a small tweak of
> the key lookup code: the first time you find the key, just return
> the map you found it in, rather than the command it's supposed to
> call.

IIUC, it's slightly more complicated than just modifying the return
value of a function. One reason is that "finding active keymaps" and
"looking up keys in keymaps" are done by different bits of the code. By
the time keys are being looked up, we don't know where they keymaps came
from anymore. Another reason is that some keymaps might not even be
stored in any variable (symbol's value cell) at all.

Anyway, here's some code that seems to work in some cases (but not when
the command was remapped, and not for finding where a mouse event is
bound). First is a helper function, then the actual function. Not the
cleanest code, but good enough for my .emacs.

(defun yf/find-object-in-variables (object &optional pred)
  "Find all symbols (variables) whose content is the same as OBJECT.
PRED defaults to `eq'"
  (unless pred (setq pred #'eq))
  (let ((result))
    (mapatoms (lambda (x)
                (when (and (boundp x)
                           (funcall pred (symbol-value x) object))
                  (push x result))))
    result))
(defun yf/key-binding-locus (key)
  "Return a list of symbols whose value is the active keymap
which holds a binding for the given KEY."
  (interactive "KKey seq: ")
  (let ((active-maps (current-active-maps t))
        map found)
    ;; we loop over active-maps like key-binding does.
    (while (not
            (setq found
                  (lookup-key
                   (setq map
                         (pop active-maps))
                   key
                   t)))
      ;; do nothing
      )
    (if (not found)
        (message "Key not found (which is weird, if you want my opinion).")
      (if (and (symbolp found) (command-remapping found))
          ;; fixme. We should mimic command-remapping ?
          (message "Found key but it got remapped and I don't know how to search that.")
        (let ((res (yf/find-object-in-variables map)))
          (if res
              (message "Found key (bound to %s) in a keymap bound to: %S"
                       found
                       res)
            (message "Found key (bound to %s) in a keymap which isn't in any variable."
                     found)))))))

-- 
Nico.





  parent reply	other threads:[~2014-06-02 10:15 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-13 20:34 bug#13948: no key-binding-locus Brian Malehorn
2013-04-23 19:41 ` Josh
2014-06-02 10:15 ` Nicolas Richard [this message]
2014-06-02 13:55   ` Stefan Monnier
2014-06-04 10:51     ` Nicolas Richard
2014-06-04 13:50       ` Stefan Monnier
2014-06-04 14:00         ` Nicolas Richard
2014-06-04 14:20           ` Stefan Monnier
2014-06-06 17:57             ` Nicolas Richard
2014-06-06 18:27               ` Stefan Monnier
2014-06-10 19:46                 ` Nicolas Richard
2014-06-10 22:24                   ` Stefan Monnier
2014-06-11 11:23                     ` Nicolas Richard
2014-06-11 18:06                       ` Stefan Monnier
2014-06-11 20:20                         ` Nicolas Richard
2014-06-11 22:00                           ` Stefan Monnier
2014-06-12  8:16                           ` Nicolas Richard
2014-06-12 16:09                     ` Nicolas Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k38zipmh.fsf@geodiff-mac3.ulb.ac.be \
    --to=theonewiththeevillook@yahoo.fr \
    --cc=13948@debbugs.gnu.org \
    --cc=bmalehorn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.