unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Writing a function/macro in Elisp that generates a function at runtime
@ 2008-10-27 13:30 Toby Cubitt
  2008-10-28 18:09 ` Johan Bockgård
  0 siblings, 1 reply; 8+ messages in thread
From: Toby Cubitt @ 2008-10-27 13:30 UTC (permalink / raw)
  To: help-gnu-emacs


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.




^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <mailman.2216.1225168079.25473.help-gnu-emacs@gnu.org>]

end of thread, other threads:[~2008-10-28 18:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-27 13:30 Writing a function/macro in Elisp that generates a function at runtime Toby Cubitt
2008-10-28 18:09 ` Johan Bockgård
2008-10-28 17:21   ` Toby Cubitt
     [not found] <mailman.2216.1225168079.25473.help-gnu-emacs@gnu.org>
2008-10-28  5:56 ` Barry Margolin
2008-10-28 10:38   ` Toby Cubitt
2008-10-28 12:42     ` Toby Cubitt
     [not found]     ` <mailman.2265.1225201433.25473.help-gnu-emacs@gnu.org>
2008-10-28 16:47       ` Ted Zlatanov
2008-10-28 17:10         ` Toby Cubitt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).