From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joe Riel Newsgroups: gmane.emacs.help Subject: Re: Rebind key and save/reuse previous bound function Date: Sun, 1 Nov 2015 14:08:26 -0800 Message-ID: <20151101140826.45307f37@gauss> References: <20151029214551.1cf59d18@gauss> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1446415734 31113 80.91.229.3 (1 Nov 2015 22:08:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 1 Nov 2015 22:08:54 +0000 (UTC) Cc: Help GNU Emacs To: John Mastro Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Nov 01 23:08:45 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zt0ns-0002Kd-Io for geh-help-gnu-emacs@m.gmane.org; Sun, 01 Nov 2015 23:08:44 +0100 Original-Received: from localhost ([::1]:39054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zt0ns-0003Ir-0c for geh-help-gnu-emacs@m.gmane.org; Sun, 01 Nov 2015 17:08:44 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55998) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zt0ni-0003HY-4q for help-gnu-emacs@gnu.org; Sun, 01 Nov 2015 17:08:34 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zt0nd-0004fL-5j for help-gnu-emacs@gnu.org; Sun, 01 Nov 2015 17:08:34 -0500 Original-Received: from cdptpa-outbound-snat.email.rr.com ([107.14.166.231]:11623 helo=cdptpa-oedge-vip.email.rr.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zt0nd-0004fG-1l for help-gnu-emacs@gnu.org; Sun, 01 Nov 2015 17:08:29 -0500 Original-Received: from [75.80.189.125] ([75.80.189.125:44358] helo=gauss) by cdptpa-oedge03 (envelope-from ) (ecelerity 3.5.0.35861 r(Momo-dev:tip)) with ESMTP id 4A/E3-21549-B5D86365; Sun, 01 Nov 2015 22:08:28 +0000 In-Reply-To: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-RR-Connecting-IP: 107.14.168.142:25 X-Cloudmark-Score: 0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 107.14.166.231 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107894 Archived-At: On Sat, 31 Oct 2015 13:22:29 -0700 John Mastro wrote: > Joe Riel 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