From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Florian Beck Newsgroups: gmane.emacs.help Subject: Re: replacing a function with another one Date: Wed, 12 Mar 2014 20:34:37 +0100 Message-ID: <5320B6CD.5000306@miszellen.de> References: <87vbvofsi6.fsf@yun.yagibdah.de> <87bnxgs4r9.fsf@web.de> <87lhwj1cfz.fsf@yun.yagibdah.de> <87zjkz6vd5.fsf@web.de> <8738ir161u.fsf@yun.yagibdah.de> <87eh2b6nfm.fsf@web.de> <87r46anab5.fsf@yun.yagibdah.de> <87vbvleiey.fsf@yun.yagibdah.de> <87vbvlbm8p.fsf@web.de> <87siqpsdgs.fsf@yun.yagibdah.de> <87iorljkhx.fsf@web.de> <87pplsivy7.fsf@yun.yagibdah.de> <87a9cwtj76.fsf@web.de> <87iorjmidz.fsf@yun.yagibdah.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1394653271 7619 80.91.229.3 (12 Mar 2014 19:41:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Mar 2014 19:41:11 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Mar 12 20:41:21 2014 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 1WNp1l-0002aw-Du for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Mar 2014 20:41:21 +0100 Original-Received: from localhost ([::1]:34445 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNp1l-0004rX-1l for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Mar 2014 15:41:21 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42614) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNp1O-0004cU-5g for help-gnu-emacs@gnu.org; Wed, 12 Mar 2014 15:41:03 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNp1I-0004xW-61 for help-gnu-emacs@gnu.org; Wed, 12 Mar 2014 15:40:57 -0400 Original-Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::3]:38310) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNp1H-0004xQ-PA for help-gnu-emacs@gnu.org; Wed, 12 Mar 2014 15:40:52 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; t=1394653250; l=1589; s=domk; d=streitblatt.de; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:References: Subject:To:MIME-Version:From:Date:X-RZG-CLASS-ID:X-RZG-AUTH; bh=nV06OFVm2nx0wRr57KFO3PMcmRY=; b=EtYfpK9ra+wT2vr3Gg0LnTjBR50k6HkDo1K/oo+Hu50VgLXSsf/t/x1dCpYbkL/BhAN usS/bOBEBX5KfIBMLfQuumok0uP6g+lweqSJYtXxMnCylva1YwwmG9dhKE2it4nWFfyx5 VuCNLWrw7G9LAAZCNrwJaXVQDl9bOITZg/o= X-RZG-AUTH: :KmALZ0mpdbGonPxw7gDkop508XQjelhLxGYn4B74/iddlkME3ssvHN/NX3uKqg+oBr3QLb7VhqJ7 X-RZG-CLASS-ID: mo00 Original-Received: from [109.43.140.244] (ip-109-43-140-244.web.vodafone.de [109.43.140.244]) by smtp.strato.de (RZmta 32.27 DYNA|AUTH) with ESMTPSA id j04b4cq2CJYdur5 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) for ; Wed, 12 Mar 2014 20:34:39 +0100 (CET) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: <87iorjmidz.fsf@yun.yagibdah.de> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:238:20a:202:5300::3 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:96474 Archived-At: >> (lambda (&rest r) (apply FUNCTION OLDFUN r)) This line tells you what the ADVICE does (in this case the around advice). But what does it mean? 1. Once you define an advice, instead of OLDFUN, emacs calls (lambda (&rest r) (apply FUNCTION OLDFUN r)), which is a function that has any arguments and only calls (apply FUNCTION OLDFUN r) 2. &rest r means all (arbitrarily many) arguments are collected in the variable r. 3. apply then calls your FUNCTION with OLDFUN and the rest as arguments For example, define a function: (defun my-fun (a b) (list a b)) and a function we will add as an advice: (defun my-advice (&rest args) args) Add it: (advice-add 'my-fun :around 'my-advice) Now call (my-fun 'a 'b) This returns ((lambda (a b) (list a b)) 1 2) As you can see, your advice function gets called with three arguments of which the first is the original definition. With that knowledge, we can redefine our advice function: (defun my-advice (fun a b) (funcall fun a 'X)) using the first argument to call the original function. And that is really all you have to know when advising a function - define a new function that takes the old function as an additional first argument - add the advice (advice-add 'my-fun :around 'my-advice) Things are even easier with a :before advice, where you can do anything you want with the arguments: (defun my-fun2 (a b) (list a b)) (defun my-advice2 (x y) (message "List contains %s and %s." x y)) (advice-add 'my-fun2 :before 'my-advice2) -- Florian Beck