I posted this to comp.lang.emacs without thinking about here, email
preferred as I read this group infrequently (natch, I will keep an eye
out for any reply, though! :-)
It is possible to do the following with functions to extend them
(this is for an emacs program, I am not familiar with 'regular' lisp,
though I daresay there be little if any difference)
(fset 'old-foo (symbol-function 'foo))
(defun foo ()
"Just like old-foo, except more so"
(old-foo)
(more-so))
(Couresy of elisp manual)
The question I have is this:
In this particular program I am writing, I will be extending several
functions, each with the same 'extension'. To (hopefully) make things
more readable, I want to know if it is possible to create a list of
functions, and step through the list redefining, as opposed to doing
each one by hand, and if so, any brief bits of program would be
greatly appreciated.
Thank you in advance,
Stephen
--
Stephen Landamore, ·····@uk.ac.warwick.csv
University of Warwick, England, CV4 7AL.
main(_){static long i=366640679;if(i=(_=i)>>6)main();putchar((_&'?')+62);}
In article <········@csv.warwick.ac.uk> you write:
> I posted this to comp.lang.emacs without thinking about here, email
>preferred as I read this group infrequently (natch, I will keep an eye
>out for any reply, though! :-)
I'm sending it both places, as other readers may have comments on my
solution.
> It is possible to do the following with functions to extend them
>(this is for an emacs program, I am not familiar with 'regular' lisp,
>though I daresay there be little if any difference)
>
>(fset 'old-foo (symbol-function 'foo))
>(defun foo ()
> "Just like old-foo, except more so"
> (old-foo)
> (more-so))
>
> The question I have is this:
> In this particular program I am writing, I will be extending several
>functions, each with the same 'extension'. To (hopefully) make things
>more readable, I want to know if it is possible to create a list of
>functions, and step through the list redefining, as opposed to doing
>each one by hand, and if so, any brief bits of program would be
>greatly appreciated.
Since Emacs Lisp lacks closures, doing this is kind of tricky.
I'm assuming that you have a list of functions, and you want to add
(more-so) to the end of all of them, just as in the above example. It gets
more complicated if you want to make different modifications to each of
them, although you should be able to derive it from the basic idea here.
It also assumes that none of the functions are interactive.
(defvar *list-of-functions-to-redefine*
'(foo bar baz quux))
(defun redefine-function (function-name)
(let ((old-definition (symbol-function function-name)))
(fset function-name
(` (lambda (&rest args)
(apply '(, old-definition) args)
(more-so))))))
(mapcar 'redefine-function *list-of-functions-to-redefine*)
Here's how to write redefine-function without using eval:
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar
From: Mark Kantrowitz
Subject: Re: List / function info wanted
Date:
Message-ID: <BusJFr.602.1@cs.cmu.edu>
I would caution against redefining any of the built-in functions this
way -- it may work fine in interpreted code, but not necessarily so in
compiled code. The compiler is not required to look at the function
cell for Lisp primitives like CAR.
--mark
In article <········@csv.warwick.ac.uk> ·····@warwick.ac.uk (Stephen Landamore) writes:
(fset 'old-foo (symbol-function 'foo))
(defun foo ()
"Just like old-foo, except more so"
(old-foo)
(more-so))
... I want to know if it is possible to create a list of
functions, and step through the list redefining, as opposed to doing
each one by hand, and if so, any brief bits of program would be
greatly appreciated.
Stephen Landamore, ·····@uk.ac.warwick.csv
(mapcar '(lambda (name)
(fset (intern (concat "old-" (symbol-name name)))
(symbol-function name))
(eval
(list 'defun name nil
(concat "Just like old-" (symbol-name name)
", except more so")
(list (intern (concat "old-" (symbol-name name))))
'(more-so))))
'(foo1 foo2 foo3))
You can clean this up considerably, but as it stands it is a pretty
much direct mapping of your original code.
doug
--------------------------------------------------------------------
Doug Morgan, ····@ads.com, (415) 960-7300
Advanced Decision Systems (a division of Booz-Allen & Hamilton Inc.)
1500 Plymouth St., Mountain View, CA 94043-1230
--------------------------------------------------------------------