1

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

Drew
  • 75,699
  • 9
  • 109
  • 225
untore
  • 131
  • 1
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Feb 01 '21 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 Feb 01 '21 at 20:48

1 Answers1

2

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.

NickD
  • 27,023
  • 3
  • 23
  • 42