2

If I get the function definition with

(setq wrapped-copy (symbol-function 'fn))

how can I make a copy such that I can redefine the same function with a wrapped version? like

(fset 'fn #'(lambda () (wrapped-copy)))

I tried with copy-seq but I think it copies by reference, so the anon function looses the function definition in the wrapped copy

2
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Feb 1, 2021 at 20:45
  • Unclear what you mean by "redefine" and "wrapped version". fset lets you define an alias, like defalias does. And you're using a variable whose value is a function in function position. Elisp is a Lisp 2, not a Lisp 1, so you would need (lambda () (funcall wrapped-copy)). But as I say, to me it's not clear what you're asking.
    – Drew
    Commented Feb 1, 2021 at 20:48

1 Answer 1

3

Use funcall or apply:

(setq wrapped-copy (symbol-function 'emacs-version))
(fset 'fn (lambda () (funcall wrapped-copy)))

(fn)
"GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32, cairo version 1.16.0)
 of 2020-06-15"

Although, in general, I would use an advice for wrapping a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.