[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Rebind key and save/reuse previous bound function
From: |
Joe Riel |
Subject: |
Re: Rebind key and save/reuse previous bound function |
Date: |
Sun, 1 Nov 2015 14:08:26 -0800 |
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
- Re: Rebind key and save/reuse previous bound function,
Joe Riel <=