From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: Rebind key and save/reuse previous bound function Date: Sat, 31 Oct 2015 13:22:29 -0700 Message-ID: References: <20151029214551.1cf59d18@gauss> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1446322982 3512 80.91.229.3 (31 Oct 2015 20:23:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 31 Oct 2015 20:23:02 +0000 (UTC) To: Help GNU Emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 31 21:23:02 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 1Zscg1-0000kt-UZ for geh-help-gnu-emacs@m.gmane.org; Sat, 31 Oct 2015 21:23:02 +0100 Original-Received: from localhost ([::1]:56901 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zscg1-000454-2z for geh-help-gnu-emacs@m.gmane.org; Sat, 31 Oct 2015 16:23:01 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zscfq-00044q-Nt for help-gnu-emacs@gnu.org; Sat, 31 Oct 2015 16:22:51 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zscfq-0001eS-1C for help-gnu-emacs@gnu.org; Sat, 31 Oct 2015 16:22:50 -0400 Original-Received: from mail-yk0-x22c.google.com ([2607:f8b0:4002:c07::22c]:36280) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zscfp-0001eO-TH for help-gnu-emacs@gnu.org; Sat, 31 Oct 2015 16:22:49 -0400 Original-Received: by ykba4 with SMTP id a4so106524084ykb.3 for ; Sat, 31 Oct 2015 13:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=L7MuM5h6d/p6P+fXN3AlMV5DzZ+xi269DhOupqQ0zqE=; b=0HP5jXubTFg0yVB8dYY9bu8uyRqbUt/sVT98BbGdXws3k5g1ufsCNwghrco0yDDtLV JwBReIeKt7gRnMAMux+i0try0mHNGtS5aBc9JPUAWA4gfuiuigusrNNNATnjmfna2isk XGtXuLGpDlLZUaOrdQAY/rZIJhMU1YYqXX8OGjQmwb+zk8PviRXQAFg5opQxfSFiXDQn vt5ZlhGzu3xz7N64U3eBUdCILRvtSSVm/jwhYEdGz3UG4TvXmy4748ixZRwxQUfRMpYd vsvUoRayRPrfRKVwdvLPwc2FBwQDgZgGjzSTxrn+vnmYTttaN/AZaIX+NxE9UhAJ5SBL QelA== X-Received: by 10.129.156.133 with SMTP id t127mr11010057ywg.60.1446322969294; Sat, 31 Oct 2015 13:22:49 -0700 (PDT) Original-Received: by 10.37.75.70 with HTTP; Sat, 31 Oct 2015 13:22:29 -0700 (PDT) In-Reply-To: <20151029214551.1cf59d18@gauss> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4002:c07::22c 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:107880 Archived-At: 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. -- john