Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the function by the keymap?

19 views
Skip to first unread message

Adam Jiang

unread,
Nov 10, 2012, 11:09:02 AM11/10/12
to help-gn...@gnu.org
Hi,

I am working on a minor mode in which I want to remap [backspace] key to different functions in different situation. The lisp code looks like

(defun smart-operator-backspace ()
  (interactive)
  (cond ((looking-back "hello"))
     (save-excursion
        (replace-regexp-in-string ".*hello" "" (match-string 1))))
     (t
        (delete-char 1))))


However, (delete-char) never works for me. The reason is another minor mode remapped the backspace key to function 'autopair-backspace'. Then, I realized that I need a method to fetch the function by given keymap and get it called rather thant have hardcoded deletion executed.

How can I do that with elisp code?

/Adam

Eli Zaretskii

unread,
Nov 10, 2012, 11:38:51 AM11/10/12
to help-gn...@gnu.org
> Date: Sun, 11 Nov 2012 01:09:02 +0900
> From: Adam Jiang <jiang...@gmail.com>
>
> (defun smart-operator-backspace ()
> (interactive)
> (cond ((looking-back "hello"))
> (save-excursion
> (replace-regexp-in-string ".*hello" "" (match-string 1))))
> (t
> (delete-char 1))))
>
>
> However, (delete-char) never works for me. The reason is another minor mode
> remapped the backspace key to function 'autopair-backspace'. Then, I
> realized that I need a method to fetch the function by given keymap and get
> it called rather thant have hardcoded deletion executed.
>
> How can I do that with elisp code?

I think you want lookup-key or local-key-binding.

Adam Jiang

unread,
Nov 11, 2012, 11:27:09 PM11/11/12
to help-gn...@gnu.org
2012/11/11 Le Wang <l26...@gmail.com>
On Sun, Nov 11, 2012 at 12:09 AM, Adam Jiang <jiang...@gmail.com> wrote:
> However, (delete-char) never works for me. The reason is another minor mode
> remapped the backspace key to function 'autopair-backspace'. Then, I
> realized that I need a method to fetch the function by given keymap and get
> it called rather thant have hardcoded deletion executed.
>
> How can I do that with elisp code?

You don't want to dig through the keymaps yourself.  Just take your
minor-mode out of the lookup chain and use `key-binding'.

This is what I do here:

https://github.com/lewang/rebox2/blob/master/rebox2.el#L2363

The code works like a charm. Thank you, Le.
 

The deeper problem is that I have a chain of 3 functions from
different minor-modes all bound to backspace.  They share the key
happily, but Emacs lacks the mechanism to document this calling chain
when I ask about <BACKSPACE> with C-h k.  I described the need to
solve this as a feature request
http://lists.gnu.org/archive/html/help-gnu-emacs/2012-10/msg00465.html
, but there wasn't much interest.  :(
 
It seems this is a good idea. I'd like to help but I am pretty new to elisp. I'll try to get some experience about the keybinding-chain thing first.

Best regards,
/Adam

--
Le
0 new messages