From: John Mastro <john.b.mastro@gmail.com>
To: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Rebind key and save/reuse previous bound function
Date: Sat, 31 Oct 2015 13:22:29 -0700 [thread overview]
Message-ID: <CAOj2CQTnQaCYm69DM0uL4Cvb9QXfSs3-xjY=h_a5=XiftRgjDQ@mail.gmail.com> (raw)
In-Reply-To: <20151029214551.1cf59d18@gauss>
Joe Riel <joer@san.rr.com> wrote:
>
> Is there a clean way save the function bound to a key
> so that a new function can be created that is bound
> to the same key, does something, then executes the
> original bound function?
I don't think there's anything built-in that does exactly what you
describe, but there are a few alternatives for doing it and all are
pretty simple.
Something like this would probably be the most direct translation of
your description:
(defun define-before (map key cmd)
(let ((old (lookup-key map key)))
(define-key map key
(if (null old)
cmd
(lambda ()
(interactive)
(call-interactively cmd)
(call-interactively old))))))
(define-before global-map (kbd "C-n")
(lambda ()
(interactive)
(message "Going to the next line!")))
However, I think using "before advice" is a nicer solution for this,
because it integrates well with `describe-function'.
(defun my-next-line-advice (&rest args)
(message "Going to the next line!"))
(advice-add 'next-line :before #'my-next-line-advice)
The arguments your advice function receives will be the same as those
for the original function you're advicing, if any. Check the
documentation for `add-function' for a description of all the different
kinds of advice and what arguments the advicing function receives.
--
john
next prev parent reply other threads:[~2015-10-31 20:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-30 4:45 Rebind key and save/reuse previous bound function Joe Riel
2015-10-30 4:57 ` Joe Riel
2015-10-31 16:33 ` Emanuel Berg
2015-10-30 4:58 ` Joe Riel
2015-10-30 18:31 ` Doug Lewan
2015-10-31 20:22 ` John Mastro [this message]
2015-11-01 22:08 ` Joe Riel
2015-11-02 0:22 ` Emanuel Berg
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='CAOj2CQTnQaCYm69DM0uL4Cvb9QXfSs3-xjY=h_a5=XiftRgjDQ@mail.gmail.com' \
--to=john.b.mastro@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/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.