help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Writing a function/macro in Elisp that generates a function at runtime


From: Toby Cubitt
Subject: Writing a function/macro in Elisp that generates a function at runtime
Date: Mon, 27 Oct 2008 13:30:13 +0000
User-agent: Thunderbird 2.0.0.17 (X11/20080929)

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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]