From: Joe Riel <joer@san.rr.com>
To: John Mastro <john.b.mastro@gmail.com>
Cc: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Rebind key and save/reuse previous bound function
Date: Sun, 1 Nov 2015 14:08:26 -0800 [thread overview]
Message-ID: <20151101140826.45307f37@gauss> (raw)
In-Reply-To: <CAOj2CQTnQaCYm69DM0uL4Cvb9QXfSs3-xjY=h_a5=XiftRgjDQ@mail.gmail.com>
On Sat, 31 Oct 2015 13:22:29 -0700
John Mastro <john.b.mastro@gmail.com> wrote:
> 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.
Thanks. Another user also suggested using advice.
I followed that advice 8-).
--
Joe Riel
next prev parent reply other threads:[~2015-11-01 22:08 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
2015-11-01 22:08 ` Joe Riel [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151101140826.45307f37@gauss \
--to=joer@san.rr.com \
--cc=help-gnu-emacs@gnu.org \
--cc=john.b.mastro@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.
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).