From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: Writing a function/macro in Elisp that generates a function at runtime Date: Tue, 28 Oct 2008 01:56:10 -0400 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1225176044 19669 80.91.229.12 (28 Oct 2008 06:40:44 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 28 Oct 2008 06:40:44 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 28 07:41:46 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KuiGw-0007gr-A3 for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 07:41:46 +0100 Original-Received: from localhost ([127.0.0.1]:48774 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KuiFq-0008AS-7H for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 02:40:38 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!news2!news.eternal-september.org!news.motzarella.org!motzarella.org!registered.motzarella.org!barmar Original-Newsgroups: gnu.emacs.help Original-Lines: 49 Original-X-Trace: registered.motzarella.org U2FsdGVkX1/iEZmKCmgvb0knQ4opSYdCnanzakfhakqzgnT87I/LFJ8G7DeZcl/2+ih35xRGGMV0FyIfutP49JR0LgsPms1tN/iOehFk6GDu+f8CozXxpZJ42BgNSL7h62ukVaRGrGVLT1zQV3N1Zg== Original-X-Complaints-To: Please send complaints to abuse@motzarella.org with full headers Original-NNTP-Posting-Date: Tue, 28 Oct 2008 05:56:10 +0000 (UTC) X-Auth-Sender: U2FsdGVkX19lXeOQTtzMmErEVH839m9L2dasth7Xpac= Cancel-Lock: sha1:vFR9IJwEa5JcU6WANborFhSBeLI= User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Mail-Copies-To: nobody Original-Xref: news.stanford.edu gnu.emacs.help:163858 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:59199 Archived-At: In article , Toby Cubitt wrote: > Hi, > > I'm trying to write a function in Elisp that generates a function at > runtime, but hitting the limits of my Lisp abilities. Here's a first > attempt at the kind of thing I'm trying to do: > > (defun wrap-insert-function (insfun) > `(lambda (new-data old-cell) > (setf (cell-data old-cell) > (funcall ,insfun new-data (cell-data old-cell))) > old-cell)) > > I want to use this to wrap functions that don't know anything about the > "cell" data structures, so they can operate on them: > > (setq wrapped-insert-function > (wrap-insert-function > (some-complicated-code-that-returns-a-suitable-function))) > > (funcall wrapped-insert-function data cell) > > > The problem is, because it's quoted, the `setf' macro never gets > expanded by the byte-compiler. And that's no good, because the cl > package where `setf' is defined shouldn't be loaded at runtime, and To do that you'd need lexical closures, but Elisp doesn't have them. Since you're not generating the function until runtime, there's nothing to compile at compile time. > anyway we want it to be expanded at compile-time, not at run-time as > that would be ugly and inefficient. You don't need to generate a function on the fly for this. Do something like this: (defun insert-with-function (insfun new-data old-cell) (setf (cell-data old-cell) (funcall insfun new-data (cell-data old-cell))) old-cell) -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***