From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Toby Cubitt Newsgroups: gmane.emacs.help Subject: Writing a function/macro in Elisp that generates a function at runtime Date: Mon, 27 Oct 2008 13:30:13 +0000 Message-ID: <4905C265.1080907@dr-qubit.org> Reply-To: Toby Cubitt NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1225168146 2970 80.91.229.12 (28 Oct 2008 04:29:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 28 Oct 2008 04:29:06 +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 05:30:08 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 1KugDX-00084t-Kz for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 05:30:07 +0100 Original-Received: from localhost ([127.0.0.1]:60788 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KugCR-0004RF-Ec for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 00:28:59 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KuSAw-0001so-3R for help-gnu-emacs@gnu.org; Mon, 27 Oct 2008 09:30:30 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KuSAu-0001s8-Bc for help-gnu-emacs@gnu.org; Mon, 27 Oct 2008 09:30:29 -0400 Original-Received: from [199.232.76.173] (port=53432 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KuSAu-0001s5-3b for help-gnu-emacs@gnu.org; Mon, 27 Oct 2008 09:30:28 -0400 Original-Received: from mail.geekisp.com ([216.168.135.169]:7396 helo=starfish.geekisp.com) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KuSAt-0002Jy-KC for help-gnu-emacs@gnu.org; Mon, 27 Oct 2008 09:30:28 -0400 Original-Received: (qmail 17006 invoked by uid 1003); 27 Oct 2008 13:30:17 -0000 Original-Received: from [192.168.2.9] (localhost.geekisp.com [127.0.0.1]) by localhost.geekisp.com (tmda-ofmipd) with ESMTP; Mon, 27 Oct 2008 09:30:15 -0400 User-Agent: Thunderbird 2.0.0.17 (X11/20080929) X-Delivery-Agent: TMDA/1.1.11 (Ladyburn) X-Primary-Address: toby@dr-qubit.org X-detected-operating-system: by monty-python.gnu.org: OpenBSD 3.0-3.9 X-Mailman-Approved-At: Tue, 28 Oct 2008 00:27:57 -0400 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:59196 Archived-At: 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 anyway we want it to be expanded at compile-time, not at run-time as that would be ugly and inefficient. There has got to be some canonical way to do this in Lisp, but I've tried playing around with various macro/function definitions, searching the web, and reading Lisp books, without getting any good ideas. The vital clue might well be in one of the chapters on advanced Lisp macro magic (e.g. Paul Graham's "On Lisp"). But if so, then spotting the clue is beyond my current Lisp skills, and I could use a guru to point me in the right direction. Any gurus out there? Toby PS: I've tried to simplify my requirements down to a concise example, but I apologize in advance if there's some additional constraint I've forgotten to mention, and which only occurs to me later when I realise a proposed solution won't work.