1

I am trying to modify a package by redefining a function in it. However, I would like to call the original definition in the new definition. (kind of like extending a constructor in a subclass now that I think of it)

let's say we have this defined

(defun hello ()
  "hello")

I want to define something like this

(defun hello-world ()
  (concat (hello) " world"))

except... I want to name the second function hello instead of hello world, and just redefine it so that it says " world" at the end, and does that by calling the old function.

Is there a way to give the old function a new name, and then call the old function by its new name, in the new function, which uses the old name?

;; first rename hello to _hello somehow

(defun hello ()
  (concat (_hello) " world"))

or is there an even better way to accomplish the same thing?

2

1 Answer 1

2

You apparently want to advise the function hello, so that it does what you want instead.

See the Elisp manual, node Advising Functions.

This code advises function hello, replacing its definition. The resulting function invokes the original function (hello), concatenates the result of that invocation to " world", and returns that resulting string.

(advice-add 'hello :around #'my-hello)

(defun my-hello (original-function)
  "..."
  (concat (funcall original-function) " world")

(If the original function itself accepted arguments then you would add them to the list of args to my-hello and to the funcall sexp.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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